aspose file tools*
The moose likes XML and Related Technologies and the fly likes DocumentBuilder.parse not working for String input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DocumentBuilder.parse not working for String input" Watch "DocumentBuilder.parse not working for String input" New topic
Author

DocumentBuilder.parse not working for String input

Steve McRoberts
Greenhorn

Joined: Jun 28, 2011
Posts: 5
I'm trying to do a server-side xsl transform of an xml string.
But, no matter what I try, I can't get the xml string into a Document. I don't get an error, but the Document is always empty; somehow the parse is not taking the String.
Here is the pertinent code:



After running this, when I examine the contents of document's internal StringBuffer it is null.
What irks me is that at one point I did have the magical combination that made this work, but I lost the code.
Can someone help me with this, or show me a better way? Eventually I just need to be able to take an xml string and an xsl string and do the transform on the servlet and send out a finished HTML to a mobile device.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You may be running into a common trap.

Document nodes return null for the nodeValue - see the very useful table in the org.w3c.dom.Node JavaDocs.

If you didn't get a parse error, your Document is probably ok, you just need to use the various Document, Element and Node methods to get at the contents.

Bill
Steve McRoberts
Greenhorn

Joined: Jun 28, 2011
Posts: 5
Thanks Bill, but I don't think that's it. It's not just that the document appears empty, but when I use it I get the Browser error "XML document must have a top level element.".

I've tried numerous ways to populate the document with the XML to no avail. Currently I'm using the stringToDom utility I found here at the ranch. Here's the complete class:



The stringToDom method is defined as:


And I'm calling it from a servlet as follows:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Steve McRoberts wrote:I'm trying to do a server-side xsl transform of an xml string.


In which case I don't understand why you are trying to parse the XML into a Document...
Steve McRoberts
Greenhorn

Joined: Jun 28, 2011
Posts: 5
Paul Clapham wrote:
Steve McRoberts wrote:I'm trying to do a server-side xsl transform of an xml string.


In which case I don't understand why you are trying to parse the XML into a Document...


Paul, it's because the Transformer.transform takes a Source as an input, and I'm using DOMSource() to obtain the Source, and it requires a document.
Is there a short-cut I'm missing? Can I get transform() to take my xml-string directly as a Source?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
You've to make good the transformation!
"<xsl:value-of select=\"local-name()\" />"+
should be at least read as this
"<xsl:value-of select=\"local-name(*)\" />"+
if you want to read out the "doc", even though it is really awkward, but at least it should work that way.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Steve McRoberts wrote:Paul, it's because the Transformer.transform takes a Source as an input, and I'm using DOMSource() to obtain the Source, and it requires a document.
Is there a short-cut I'm missing? Can I get transform() to take my xml-string directly as a Source?


I wouldn't call it a "short-cut"; look at the documentation for Source and you'll see three classes which implement it. You chose DOMSource but look at the others. StreamSource? There's a StreamSource constructor which accepts a Reader. Now go on over to the documentation for Reader and scroll down about... there's a thing called StringReader.
Steve McRoberts
Greenhorn

Joined: Jun 28, 2011
Posts: 5
g tsuji wrote:You've to make good the transformation!
"<xsl:value-of select=\"local-name()\" />"+
should be at least read as this
"<xsl:value-of select=\"local-name(*)\" />"+
if you want to read out the "doc", even though it is really awkward, but at least it should work that way.

Thanks, g! This gets me further. I never would've suspected a typo in the xsl! So, it evidently is taking my XML, because I see "doc" in the new Browser error:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://localhost:8080/RawsM/Detail'. Line 1, Position...

<?xml version="1.0" encoding="UTF-8"?>doc
--------------------------------------^


The thing is: I don't want the browser to treat this as an XML file. I want it to be html: it need not be aware that the html was generated from xml/xsl. For now, I just want it to spit out the tag-names and their values, such as:
name Steve


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

The browser is correct, your output isn't well-formed XML. But if you want the browser to treat it as HTML, you should set the content type of the response to text/html.

Edit: You might also consider not writing out the prolog, and you might even consider producing well-formed HTML. Check out the <xsl:output> element for useful features.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
That kind of error is inevitable if you stick to that kind of primitive defective xsl. The minimum proper rendering can be this.

Then you should have at least a proper html test case, however primitive one can still blame it!
Steve McRoberts
Greenhorn

Joined: Jun 28, 2011
Posts: 5
This community rocks! Thanks to everyone for the great response! Together you have resolved my issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DocumentBuilder.parse not working for String input