Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing XML from a String

 
Dave Trower
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to use the java XML parser to to parse an XML document that is contained in a String?
Currentlt my program creates a temp,xml file from the String variable and the uses the SAX parser to parse the file but I am sure there is a better way to do this.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing. Check out if this FAQ for source code.

- m
 
aravind yarram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a StrignReader out of the XML in the String

Reader is=new StringReader(xmlString)

parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schemaFileLocation);
parser.setFeature( "http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",false);
parser.setContentHandler(yourContentHandler);
parser.setErrorHandler(yourErrorHandler);
parser.setFeature( "http://xml.org/sax/features/validation",VALIDATION);
parser.setFeature( "http://xml.org/sax/features/namespaces",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature( "http://apache.org/xml/features/validation/schema-full-checking",true);
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
parser.setFeature("http://apache.org/xml/features/validation/dynamic", false);

parser.parse(new InputSource(is));
 
Dave Trower
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Your suggestion worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic