Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml parsing from a string

 
Jason Mill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to java but making good progress, though still having trouble knowing which api to use for what. I'm trying to extract xml data from a string, the string being the contents of an xml file.
I'm trying to use a SAXParser (xerces) but the xmlString below is expecting the url where the xml file is.
SAXParser xr = new SAXParser();
xr.setContentHandler( new XmlParser() );
xr.parse(xmlString);
Unfortunatly I can't access the url directly from this code. What can I use to parse a string of xml data?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the XML forum.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the parse() signatures available on SAXParser. A couple of them take InputStream parameters. Can we turn your String into an InputStream? InputStream docs show a number of subclasses. ByteArrayInputStream can be constructed with a byte[] and we can turn String into byte[]. So I'd give it a try with:
parse( new ByteArrayInputStream( myString.getBytes() ) );
Hope that helps. (Hope it works!)
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SAXParser.parse(...) also accepts an InputSource:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic