Click to See Complete Forum and Search --> : Problem is XML parsing!


sumane
04-27-2007, 07:32 AM
Hi,

I am getting some problem while parsing a String XML in to Document object.


public static Document converDocument(String xml)
{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document document = null;
try {
document = builder.parse(new InputSource(new StringReader(xml)));
System.out.println("Document is after parsing ====>>>"+document);

} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return document;

}



The xml which I am passing is :



<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>abc</body>
</note>




It is giving the document as null;
Can anybody please help?

Khalid Ali
04-27-2007, 09:31 PM
Nothing wrong with your code, if you want to see the nodes, then go to next node which is note and get its name...
System.out.println("Document is after parsing ====>>>"+document.getFirstChild().getNodeName());

agent_x91
04-30-2007, 09:19 AM
Spelling "convert" incorrectly in the method name really isn't going to help when you need to reference the method later on.