![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Problems with JDOM [Thread Next] Re: Problems with JDOMTo: NULL Date: 3/5/2006 1:40:00 PM zhengwt@g... 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));
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
