org.w3c.dom.Document "rendered" in JSP.

D

Daniel Pitts

Is there an easy way to take a DOM object (such as a Document or
Element), and render it in a JSP? Specifically, I want to inline part
of a Document inside of an xml page created by a JSP.
 
A

Arne Vajhøj

Daniel said:
Is there an easy way to take a DOM object (such as a Document or
Element), and render it in a JSP? Specifically, I want to inline part
of a Document inside of an xml page created by a JSP.

Render it as what ?

JSTL supports XML.

Arne
 
T

Tom Anderson

Is there an easy way to take a DOM object (such as a Document or
Element), and render it in a JSP? Specifically, I want to inline part
of a Document inside of an xml page created by a JSP.

There's nothing in the DOM interface to do it - no "toXML" method or
similar.

You have to create an object which will take the DOM tree and render it to
XML. I did this recently, but can't remember quite how, and the code is in
my other trousers. I think the relevant class came from javax.xml. Google
will find what you need.

One annoyance i came across is that the DOM tree doesn't record the
document's doctype; there's a method for getting it, but the
implementation i was using returned null (which is explicitly allowed, and
i suspect quite common - there's no way for a parser using a pluggable DOM
implementation to set it). This meant that there was no way to make a
valid XML document from it, since such a thing requires a doctype. Cue
colossal parsing errors on the other side ...

That shouldn't matter in your situation, though.

tom
 
C

Chris Riesbeck

Tom said:
There's nothing in the DOM interface to do it - no "toXML" method or
similar.

You have to create an object which will take the DOM tree and render it
to XML. I did this recently, but can't remember quite how, and the code
is in my other trousers. I think the relevant class came from javax.xml.
Google will find what you need.

Look for a null transformer, e.g.,

trans = TransformerFactory.newInstance().newTransformer();
trans .setOutputProperty(OutputKeys.METHOD, "html");
trans.transform(new DOMSource(document), new StreamResult(writer));
 
R

Roedy Green

Is there an easy way to take a DOM object (such as a Document or
Element), and render it in a JSP? Specifically, I want to inline part
of a Document inside of an xml page created by a JSP.

What do you want to see? indented XML, data without tags? pretty
printed XML (tags, strings etc different colours).

Have a look at http://mindprod.com/jgloss/xml.html for what I mean by
pretty XML.

In the simplest case you could encase it in <pre>
with entity replacement using
http://mindprod.com/products1.html/ENTITIES
 
D

Daniel Pitts

Roedy said:
What do you want to see? indented XML, data without tags? pretty
printed XML (tags, strings etc different colours).

Have a look at http://mindprod.com/jgloss/xml.html for what I mean by
pretty XML.

In the simplest case you could encase it in <pre>
with entity replacement using
http://mindprod.com/products1.html/ENTITIES
I guess rendered is the wrong term.

I want the text representation of the original document to be inserted
inline.

Something along the lines of:
doc:
<?xml version="1.0" encoding="UTF-8"?>
<OriginalDocument>
<SomeStuff> has stuff in it <Boo/> </SomeStuff>
</OriginalDocument>

jsp:
<?xml version="1.0" encoding="UTF-8"?>
<JspDocument>
${someElOrTaglib(myOriginalDocument)}
</JspDocument>

output:
<?xml version="1.0" encoding="UTF-8"?>
<JspDocument>
<OriginalDocument>
<SomeStuff> has stuff in it <Boo/> </SomeStuff>
</OriginalDocument>
</JspDocument>

Does that make sense? I'm not rendering an HTML document, but a XML
document meant to be consumed somewhere else.
 
A

Arne Vajhøj

Daniel said:
I want the text representation of the original document to be inserted
inline.

Something along the lines of:
doc:
<?xml version="1.0" encoding="UTF-8"?>
<OriginalDocument>
<SomeStuff> has stuff in it <Boo/> </SomeStuff>
</OriginalDocument>

jsp:
<?xml version="1.0" encoding="UTF-8"?>
<JspDocument>
${someElOrTaglib(myOriginalDocument)}
</JspDocument>

output:
<?xml version="1.0" encoding="UTF-8"?>
<JspDocument>
<OriginalDocument>
<SomeStuff> has stuff in it <Boo/> </SomeStuff>
</OriginalDocument>
</JspDocument>

Does that make sense? I'm not rendering an HTML document, but a XML
document meant to be consumed somewhere else.

For inspiration:

<%@ page language="java" contentType="text/xml" %><?xml version="1.0"
encoding="UTF-8"?>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<JspDocument>
<c:import var="xml" url="file:///C:/daniel.xml"/>
<x:transform xml="${xml}" xslt="<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:eek:utput
method='xml' omit-xml-declaration='yes' indent='yes'/><xsl:template
match='@*|node()'><xsl:copy><xsl:apply-templates
select='@*|node()'/></xsl:copy></xsl:template></xsl:stylesheet>"/>
</JspDocument>

Arne
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top