This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Parsing XML from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML from a String" Watch "Parsing XML from a String" New topic
Author

Parsing XML from a String

Dave Trower
Ranch Hand

Joined: Feb 12, 2003
Posts: 85
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

Joined: Jun 03, 2000
Posts: 5040
Sure thing. Check out if this FAQ for source code.

- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
aravind yarram
Greenhorn

Joined: Mar 28, 2003
Posts: 12
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));


SCJP2<br />BEA Certified J2EE Developer
Dave Trower
Ranch Hand

Joined: Feb 12, 2003
Posts: 85
Thank you.
Your suggestion worked.
 
 
subject: Parsing XML from a String
 
Similar Threads
How to get XML file name ?
UTF8 Exception thrown while parsing XML file using DOM
location of xsd file
Validate XML from a string instead of a file?
passing XML as string to parser method.