Problems with JDOM

Discussion in 'XML' started by zhengwt@gmail.com, Mar 5, 2006.

  1. Guest

    I used JDOM.jar to process xml-based document.

    Recently, I meet such a problem that I can not solve.
    First, I use the following code generate xml text:

    Element root = new Element("Root");
    Document doc = new Document(root);
    ... // add some elements appended to root

    XMLOutputter out = new XMLOutputter();
    out.setFormat(Format.getPrettyFormat());

    String xml = out.outputString(doc);

    The content in "xml" is something like:

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
    ...
    </Root>

    After then, I use the following code to build a DOM based on "xml":

    SAXBuilder builder = new SAXBuilder(false);
    builder.setIgnoringElementContentWhitespace(true);
    Document doc = builder.build(xml);

    The problem comes out, the exception message says:

    java.net.MalformedURLException: no protocol: <?xml version="1.0"
    encoding="UTF-8"?>
    <Root>
    ...
    </Root>

    How to solve it? Thanks in advance!
    , Mar 5, 2006
    #1
    1. Advertising

  2. wrote:


    > String xml = out.outputString(doc);
    >
    > The content in "xml" is something like:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <Root>
    > ...
    > </Root>



    > SAXBuilder builder = new SAXBuilder(false);
    > builder.setIgnoringElementContentWhitespace(true);
    > Document doc = builder.build(xml);
    >
    > The problem comes out, the exception message says:
    >
    > java.net.MalformedURLException: no protocol: <?xml version="1.0"


    Well look at the API
    <http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html#build(java.lang.String)>
    if you pass in a string to the build method then it should be the URL of
    the XML to load and not a string with XML markup.
    So you need to use e.g.
    <http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html#build(java.io.Reader)>
    and pass in a StringReader like this
    builder.build(new StringReader(xml)

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 5, 2006
    #2
    1. Advertising

  3. Don Roby Guest

    wrote:
    > I used JDOM.jar to process xml-based document.
    >
    > Recently, I meet such a problem that I can not solve.
    > First, I use the following code generate xml text:
    >
    > Element root = new Element("Root");
    > Document doc = new Document(root);
    > ... // add some elements appended to root
    >
    > XMLOutputter out = new XMLOutputter();
    > out.setFormat(Format.getPrettyFormat());
    >
    > String xml = out.outputString(doc);
    >
    > The content in "xml" is something like:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <Root>
    > ...
    > </Root>
    >
    > After then, I use the following code to build a DOM based on "xml":
    >
    > SAXBuilder builder = new SAXBuilder(false);
    > builder.setIgnoringElementContentWhitespace(true);
    > Document doc = builder.build(xml);
    >
    > The problem comes out, the exception message says:
    >
    > java.net.MalformedURLException: no protocol: <?xml version="1.0"
    > encoding="UTF-8"?>
    > <Root>
    > ...
    > </Root>
    >
    > How to solve it? Thanks in advance!
    >


    The message should give a hint, but looking at the SAXBuilder javadoc
    verifies it. If you pass the SAXBuilder build method a string, it
    expects that string to be a URI from which the content can be retrieved
    and not the content itself.

    There are overloads for build accepting java.io.InputStream or
    java.io.Reader.

    The quick fix (to at least get you on your way again) is to change the
    line loading the Document from the string to:

    Document doc = builder.build(new StringReader(xml));
    Don Roby, Mar 5, 2006
    #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.
Similar Threads
  1. Wendy S
    Replies:
    1
    Views:
    6,350
    Darren Davison
    Aug 5, 2003
  2. Andrew

    Problems with JDOM

    Andrew, Dec 8, 2003, in forum: Java
    Replies:
    3
    Views:
    439
    Keith A. Lewis
    Dec 8, 2003
  3. Bernd Oninger
    Replies:
    4
    Views:
    12,247
    GIMME
    Jun 21, 2004
  4. Tinker
    Replies:
    4
    Views:
    5,286
    Harry Bosch
    Oct 9, 2005
  5. Bernd Oninger
    Replies:
    3
    Views:
    2,871
    GIMME
    Jun 21, 2004
Loading...

Share This Page