Subjects
Home
Xalan extension functions
Fomatting question serializing DOM with pretty print
xalan with pull parser
Cannot find the declaration
Apache Xalan drop support to run on JRE 1 1 x
Why does Doctype change processing of a document
Node set to XML string via Java extensions in Xalan J: possible?
Templates/Transformers + thread safety???
Problem evaluating xpath with muliple prefix with different namespace
remove an arbitrary attribute from xsl output
Xalan3 XSLT 2 0 XPath 2 0 support?
Problem using compiled translets with Xalan !!
Xalan and jstl 1 1 problem with transform tag
NullPointer in DOM2DTM getLocalName
URIResolvers base parameter with xsltc and cascaded imports
Performance problem for Xalan J on intel dual core
Standard libraries in JAXP?
Serializing a DOM tree to XML file, customize entities replacement
Library Conflict Involving BCEL Library
A question on how users are using <xsl:message >
Kevin Cormier as a new Apache Xalan J committer
Struggling to iterate over tokenized string
Xalan count() trouble
Problem with recursive xpath
Error when switching to java 1 5
document( ' ')
Problem with Xalan2 7 0 transformation
cr/lf options
entity encoded XML
can xalan transform 2 xml using one xslt?
Xalan J JIRA defect review Monday October 16, 2006 from 2:00 to 3:30 pm ED
xsl transform with cdata section elements
xslt parameters not expanded
Weird behavior of XPath evaluate()
How to avoid <xsl:message > instruction prints stylesheet file informations ?
Cannot find SimpleTransform subdirectory after installing Xalan J
recover from document not found exceptions
jdk1 5 and Xalan jar differences?
Performance Issue
Error/Bug adding floating point numbers
XPathAPI: eval exp using nodes with default namespace
modifying xalan to output invalid XML
NullPointerException
mege two separate xml nodes into one
Is this a XALAN document identification bug?
is StylesheetRoot really java io Serializable ?
transform() fails for DOMSource but succeeds for StreamSource
Thoughts on Transformer parameter passing
HELP, Xalan and jstl 1 1 problem with transformer
Problem with XPath namespace axis?
string utils:replace deleting search string if replacement string is an HTML
help with enumeration values pls
xalan 2 5 1 vs 2 7 performance question
How to insert/update in XML document
HTML Serialization and Handling of Ampersands in HREF Attributes
XHTML link tag stripping
SystemId Unknown; Line #24; Column #49; java lang NullPointerException
xpath text() help
Apostrophe problem with xalan 2 7 0
How to set variables in XML document?
Links
Home
Oracle database error code ...
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Converting DOM to text

Converting DOM to text

2003-01-20       - By Sean Leblanc
Reply:     1     2     3     4  

Hello,

I am trying to use the samples to convert DOM I already have in memory to a
string of text.

I'm looking at the DOM2DOM sample, and trying to use it, but I get this
error:

javax.xml.transform.TransformerException: org.w3c.dom.DOMException:
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not
permitted.

Here is the code. The variable "document" is passed in.

// Get ready for a transformation:
       DOMSource xmlDomSource = new DOMSource(document);
       DocumentBuilderFactory dFactory =
DocumentBuilderFactory.newInstance();
       dFactory.setNamespaceAware(true);
       DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
       TransformerFactory tFactory = TransformerFactory.newInstance();
       // Get our XSL file, transform into source:
       File xslFile = new File("test.xsl");
       System.out.println("Exists? " + xslFile.exists());
       Document xslDoc = dBuilder.parse(xslFile);
       DOMSource xslDomSource = new DOMSource(xslDoc);

       // Set the systemId: note this is actually a URL, not a local
filename
       xslDomSource.setSystemId("");

       // Process the stylesheet DOMSource and generate a Transformer.
       Transformer transformer = tFactory.newTransformer(xslDomSource);

       // Create an empty DOMResult for the Result.
       DOMResult domResult = new DOMResult();
       

               // Perform the transformation, placing the output in the
DOMResult.
       transformer.transform(xmlDomSource, domResult);

               //Instantiate an Xalan XML serializer and use it to
serialize the output DOM to System.out
               // using a default output format.
       Serializer serializer = SerializerFactory.getSerializer

(OutputProperties.getDefaultMethodProperties("xml"));
       serializer.setOutputStream(System.out);
       serializer.asDOMSerializer().serialize(domResult.getNode());
This e-mail, including attachments, is intended for the person(s) or company
named and may contain confidential and/or legally privileged information.
Unauthorized disclosure, copying or use of this information may be unlawful
and is prohibited. If you are not the intended recipient, please delete this
message and notify the sender.

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas
-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">


<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 10">
<meta name=Originator content="Microsoft Word 10">
<link rel=File-List href="cid:filelist.xml@(protected)">
<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
 <o:DoNotRelyOnCSS/>
</o:OfficeDocumentSettings>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
 <w:SpellingState>Clean</w:SpellingState>
 <w:GrammarState>Clean</w:GrammarState>
 <w:DocumentKind>DocumentEmail</w:DocumentKind>
 <w:EnvelopeVis/>
 <w:Compatibility>
  <w:BreakWrappedTables/>
  <w:SnapToGridInCell/>
  <w:WrapTextWithPunct/>
  <w:UseAsianBreakRules/>
 </w:Compatibility>
 <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
  {mso-style-parent:"";
  margin:0in;
  margin-bottom:.0001pt;
  mso-pagination:widow-orphan;
  font-size:12.0pt;
  font-family:"Times New Roman";
  mso-fareast-font-family:"Times New Roman";}
a:link, span.MsoHyperlink
  {color:blue;
  text-decoration:underline;
  text-underline:single;}
a:visited, span.MsoHyperlinkFollowed
  {color:purple;
  text-decoration:underline;
  text-underline:single;}
span.EmailStyle17
  {mso-style-type:personal-compose;
  mso-style-noshow:yes;
  mso-ansi-font-size:10.0pt;
  mso-bidi-font-size:10.0pt;
  font-family:Arial;
  mso-ascii-font-family:Arial;
  mso-hansi-font-family:Arial;
  mso-bidi-font-family:Arial;
  color:windowtext;}
span.SpellE
  {mso-style-name:"";
  mso-spl-e:yes;}
span.GramE
  {mso-style-name:"";
  mso-gram-e:yes;}
@(protected) Section1
  {size:8.5in 11.0in;
  margin:1.0in 1.25in 1.0in 1.25in;
  mso-header-margin:.5in;
  mso-footer-margin:.5in;
  mso-paper-source:0;}
div.Section1
  {page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
  {mso-style-name:"Table Normal";
  mso-tstyle-rowband-size:0;
  mso-tstyle-colband-size:0;
  mso-style-noshow:yes;
  mso-style-parent:"";
  mso-padding-alt:0in 5.4pt 0in 5.4pt;
  mso-para-margin:0in;
  mso-para-margin-bottom:.0001pt;
  mso-pagination:widow-orphan;
  font-size:10.0pt;
  font-family:"Times New Roman";}
</style>
<![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple style='tab-interval:.5in'>

<div class=Section1>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Hello,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I am trying to use the samples to convert DOM I already have
in memory to a string of text.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I'm looking at the DOM2DOM sample, and trying to use
it, but I get this error:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><span class=SpellE><font size=2 face=Arial><span
style='font-size:10.0pt;font-family:Arial'>javax.xml.transform
.TransformerException</span></font></span><font
size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>: org.w3c
.dom.DOMException:
HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not
permitted.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Here is the code. The variable "document" is
passed in.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>// Get ready for a transformation:<o:p></o:p></span></font><
/p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>DOMSource</span> <span class=SpellE>xmlDomSource</span> = new <span
class=SpellE><span class=GramE>DOMSource</span></span><span class=GramE>(</span
>document);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>DocumentBuilderFactory</span> <span class=SpellE>dFactory</span> =
<span class=SpellE><span class=GramE>DocumentBuilderFactory.newInstance</span><
/span><span
class=GramE>(</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>dFactory.setNamespaceAware</span></span><span
class=GramE>(</span>true);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>DocumentBuilder</span> <span class=SpellE>dBuilder</span> = <span
class=SpellE><span class=GramE>dFactory.newDocumentBuilder</span></span><span
class=GramE>(</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>TransformerFactory</span> <span class=SpellE>tFactory</span> =
<span
class=SpellE><span class=GramE>TransformerFactory.newInstance</span></span><span
class=GramE>(</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//
Get our XSL file, transform into source:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>File
<span class=SpellE>xslFile</span> = new <span class=GramE>File(</span>&quot;
<span
class=SpellE>test.xsl</span>&quot;);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>System.out.println</span></span><span
class=GramE>(</span>&quot;Exists? &quot; + <span class=SpellE><span
class=GramE>xslFile.exists</span></span><span class=GramE>(</span>));<o:p></o:p
></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>Document <span class=SpellE>xslDoc</span> = <span class=SpellE><span
class=GramE>dBuilder.parse</span></span><span class=GramE>(</span><span
class=SpellE>xslFile</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>DOMSource</span> <span class=SpellE>xslDomSource</span> = new <span
class=SpellE><span class=GramE>DOMSource</span></span><span class=GramE>(</span
><span
class=SpellE>xslDoc</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//
Set the <span class=SpellE>systemId</span>: note this is actually a URL, not a
local filename<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>xslDomSource.setSystemId</span></span><span
class=GramE>(</span>&quot;&quot;);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//
Process the <span class=SpellE>stylesheet</span> <span class=SpellE>DOMSource<
/span>
and generate a Transformer.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>Transformer <span class=SpellE>transformer</span> = <span class=SpellE>
<span
class=GramE>tFactory.newTransformer</span></span><span class=GramE>(</span><span
class=SpellE>xslDomSource</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>//
<span
class=GramE>Create</span> an empty <span class=SpellE>DOMResult</span> for the
Result.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>DOMResult</span> <span class=SpellE>domResult</span> = new <span
class=SpellE><span class=GramE>DOMResult</span></span><span class=GramE>(</span
>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p
></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span style='mso-spacerun:yes'>&nbsp; </span><span
style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;</span>// <span class=GramE
>Perform</span>
the transformation, placing the output in the <span class=SpellE>DOMResult<
/span>.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>transformer.transform</span></span><span
class=GramE>(</span><span class=SpellE>xmlDomSource</span>, <span class=SpellE
>domResult</span>);<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>//Instantiate an <span
class=SpellE>Xalan</span> XML <span class=SpellE>serializer</span> and use it
to serialize the output DOM to <span class=SpellE>System.out</span><o:p></o:p><
/span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span style='mso-tab-count:1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span>// using a default output
format.<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE>Serializer</span> <span class=SpellE>serializer</span> = <span
class=SpellE>SerializerFactory.getSerializer</span><o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>(<span class=SpellE><span class=GramE>OutputProperties
.getDefaultMethodProperties</span></span><span
class=GramE>(</span>&quot;xml&quot;));<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>serializer.setOutputStream</span></span><span
class=GramE>(</span><span class=SpellE>System.out</span>);<o:p></o:p></span><
/font></p>

<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><span
style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span
class=SpellE><span class=GramE>serializer.asDOMSerializer</span></span><span
class=GramE>(</span>).serialize(<span class=SpellE>domResult.getNode</span>());
<o:p></o:p></span></font></p>

</div>

</body>

</html>

<P><FONT SIZE=2 FACE="Arial">This e-mail, including attachments, is intended
for the person(s) or company named and may contain confidential and/or legally
privileged information. Unauthorized disclosure, copying or use of this
information may be unlawful and is prohibited. If you are not the intended
recipient, please delete this message and notify the sender.</FONT></P>