-4

The contents of my webpage is an XML file and I am saving this content in a string. I have to read the nodes of the XML file. How do I fetch the values of the nodes from this XML file?

Can someone please help me on this?

user3627332
  • 177
  • 1
  • 3
  • 13
  • http://stackoverflow.com/questions/8484921/how-to-convert-string-to-dom-document-object-in-java – SaurabhJinturkar May 12 '14 at 06:48
  • I tried to save the contents in an XML file and then read from it as a normal XML file, but I dont want to create a new file. Is there any way I can do this without saving the contents in a temp xml file? – user3627332 May 12 '14 at 06:48
  • http://stackoverflow.com/questions/373833/best-xml-parser-for-java?rq=1 – Smutje May 12 '14 at 06:49
  • Most XML parsers can read from a string: xpp3, commons digester, xerces (and many more). – Jason May 12 '14 at 06:49

1 Answers1

1

you can use a Document

In Java, how do I parse XML as a String instead of a file?

public static Document loadXMLFromString(String xml) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);

}

Community
  • 1
  • 1
Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26