Using xerces to parse a string of xml does not seem to work

Watsh

Usenet Archive
Joined
Nov 2, 2004
Messages
1
Reaction score
0
Hi All,

I have been trying to parse an XML string using the StringReader and InputSource interface but the document returned to me is always null. Please find the code below which i have been experimenting with:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;

public class Test {

/** Creates a new instance of Test */
public Test
{
}
public static void main(String args)
{
try {
String xml = "<a><b/></a>";

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance;
// set namespace and validating options on factory, if necessary factory.setValidating(false); factory.setIgnoringComments(true); DocumentBuilder builder = factory.newDocumentBuilder; StringReader reader = new StringReader(xml); InputSource source = new InputSource(reader); Document document = builder.parse(source); System.out.println(document.toString); } catch (Exception e) { e.printStackTrace; } } }

Any pointers on how to resolve this issue will be of great help also any tip for an alternative way is just as well fine for me.

Warm Regards,
Watsh
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Watsh said:
I have been trying to parse an XML string using the StringReader and InputSource interface but the document returned to me is always null. Please find the code below which i have been experimenting with:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;

public class Test {

/** Creates a new instance of Test */
public Test
{
}
public static void main(String args)
{
try {
String xml = "<a><b/></a>";

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance;
// set namespace and validating options on factory, if necessary factory.setValidating(false); factory.setIgnoringComments(true); DocumentBuilder builder = factory.newDocumentBuilder; StringReader reader = new StringReader(xml); InputSource source = new InputSource(reader); Document document = builder.parse(source); System.out.println(document.toString);

toString is not reliable to find out whether the document has been parsed as I think the result of toString depends on the implementation/parser used, it can return anything the parser implementor wants it to return. Check System.out.println(document.getElementsByTagName("*")) and I am sure it shows that there are elements in the document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,224
Members
248,132
Latest member
henrywalsh

Latest Threads

Top