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

Discussion in 'XML' started by Watsh, Nov 2, 2004.

  1. Watsh

    Watsh Guest

    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
    Watsh, Nov 2, 2004
    #1
    1. Advertising

  2. Watsh wrote:

    > 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 2, 2004
    #2
    1. Advertising

  3. "Watsh" <> wrote in message
    news:...
    > String xml = "<a><b/></a>";


    What happens if you prepend an XML declaration?

    /kmc
    Keith M. Corbett, Nov 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page