jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML from a String" Watch "Parsing XML from a String" New topic

Parsing XML from a String

Dave Trower
Ranch Hand

Joined: Feb 12, 2003
Posts: 86
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
aravind yarram

Joined: Mar 28, 2003
Posts: 12
Create a StrignReader out of the XML in the String

Reader is=new StringReader(xmlString)

parser.setFeature( "http://apache.org/xml/features/validation/schema",true);
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-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: 86
Thank you.
Your suggestion worked.
It is sorta covered in the JavaRanch Style Guide.
subject: Parsing XML from a String
It's not a secret anymore!