Problems with JDOM

Z

zhengwt

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!
 
M

Martin Honnen

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)
 
D

Don Roby

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));
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top