  | |  | Converting DOM to text | Converting DOM to text 2003-01-20 - By Pae Choi
Sometimes, I posted one including the code that does this job. Search the archive.
Pae
----- Original Message ----- From: Sean Leblanc To: 'xalan-j-users@(protected)' Sent: Monday, January 20, 2003 1:04 PM Subject: Converting DOM to text
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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns="http://www.w3.org/TR/REC-html40" xmlns:o = "urn:schemas-microsoft-com:office:office" xmlns:w = "urn:schemas-microsoft-com:office:word"><HEAD> <META http-equiv=Content-Type content="text/html; charset=windows-1252"> <META content=Word.Document name=ProgId> <META content="MSHTML 6.00.2600.0" name=GENERATOR> <META content="Microsoft Word 10" name=Originator><LINK href="cid:filelist.xml@(protected)" rel=File-List><!--[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>@(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; } P.MsoNormal { FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; mso -style-parent: ""; mso-pagination: widow-orphan; mso-fareast-font-family: "Times New Roman" } LI.MsoNormal { FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; mso -style-parent: ""; mso-pagination: widow-orphan; mso-fareast-font-family: "Times New Roman" } DIV.MsoNormal { FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; mso -style-parent: ""; mso-pagination: widow-orphan; mso-fareast-font-family: "Times New Roman" } A:link { COLOR: blue; TEXT-DECORATION: underline; text-underline: single } SPAN.MsoHyperlink { COLOR: blue; TEXT-DECORATION: underline; text-underline: single } A:visited { COLOR: purple; TEXT-DECORATION: underline; text-underline: single } SPAN.MsoHyperlinkFollowed { COLOR: purple; TEXT-DECORATION: underline; text-underline: single } SPAN.EmailStyle17 { COLOR: windowtext; FONT-FAMILY: Arial; mso-style-type: personal-compose; mso -style-noshow: yes; mso-ansi-font-size: 10.0pt; mso-bidi-font-size: 10.0pt; mso -ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial } SPAN.SpellE { mso-style-name: ""; mso-spl-e: yes } SPAN.GramE { mso-style-name: ""; mso-gram-e: yes } 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 style="tab-interval: .5in" vLink=purple link=blue bgColor=#ffffff> <DIV><FONT size=2>Sometimes, I posted one including the code that does this job.</FONT></DIV> <DIV><FONT size=2>Search the archive.</FONT></DIV> <DIV><FONT size=2></FONT> </DIV> <DIV><FONT size=2></FONT> </DIV> <DIV><FONT size=2>Pae</FONT></DIV> <DIV><FONT size=2></FONT> </DIV> <BLOCKQUOTE dir=ltr style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px"> <DIV style="FONT: 10pt arial">----- Original Message ----- </DIV> <DIV style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B> <A title=SeanL@(protected) href="mailto:SeanL@(protected)">Sean Leblanc</A> </DIV> <DIV style="FONT: 10pt arial"><B>To:</B> <A title=xalan-j-users@(protected) .org href="mailto:'xalan-j-users@(protected)'">'xalan-j-users@(protected)'< /A> </DIV> <DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, January 20, 2003 1:04 PM</DIV> <DIV style="FONT: 10pt arial"><B>Subject:</B> Converting DOM to text</DIV> <DIV><BR></DIV> <DIV class=Section1> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Hello,<o:p></o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; 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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; 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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><SPAN class=SpellE><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">javax.xml.transform .TransformerException</SPAN></FONT></SPAN><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; 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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Here is the code. The variable "document" is passed in.<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">// Get ready for a transformation:<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN>/ / Get our XSL file, transform into source:<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN>File <SPAN class=SpellE>xslFile</SPAN> = new <SPAN class=GramE>File(</SPAN>"<SPAN class=SpellE>test.xsl</SPAN>");<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN class=SpellE><SPAN class=GramE>System.out.println</SPAN></SPAN><SPAN class=GramE>(</SPAN>"Exists ? " + <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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN class=SpellE><SPAN class=GramE>xslDomSource.setSystemId</SPAN></SPAN><SPAN class=GramE>(</SPAN>"");<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN><o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-tab-count: 1"> </SPAN><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><o:p> </o:p></SPAN></FONT>< /P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-tab-count: 1"> </SPAN><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-tab-count: 1"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN>// using a default output format.<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </SPAN>(<SPAN class=SpellE><SPAN class=GramE>OutputProperties.getDefaultMethodProperties</SPAN></SPAN><SPAN class=GramE>(</SPAN>"xml"));<o:p></o:p></SPAN></FONT></P> <P class=MsoNormal><FONT face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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 face=Arial size=2><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"><SPAN style="mso-spacerun: yes"> </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> <P><FONT face=Arial size=2>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></BLOCKQUOTE></BODY></HTML>
|
|
 |