Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Parsing XML from a String

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure thing. Check out if this FAQ for source code.

- m
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
Your suggestion worked.
 
reply
    Bookmark Topic Watch Topic
  • New Topic