You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Sean Leblanc <Se...@e-infodata.com> on 2003/01/20 19:04:45 UTC

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.

Re: Converting DOM to text

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
Hi Sean,

The HIERARCHY_REQUEST_ERROR commonly occurs because the DOM is told to
store two element nodes at the root level.

Well-formed XML must have only one element at the top level (one root
element node).

eg this is well-formed

<?xml version="1.0">
<foo>
  <bar/>
</foo>

but this is not:

<?xml version="1.0">
<foo/>
<bar/>

I suspect your XSL mapping is outputting multiple elements at the top
level. The DOMResult object the xsl engine is trying to pass the nodes
to then raises the HIERARCHY_REQUEST_ERR. I know this from personal
experience :-)

I believe that the StreamResult class doesn't raise an error if it is
passed multiple elements at the top level, it happily generates invalid
XML output in this situation (or at least not valid as a complete xml
document, though it is a valid xml fragment).

If this is in fact the problem, and you do want your xsl to generate
multiple top-level nodes, then you can create a DOM object, a single
root element, then create the DOMResult so that it adds the XSL output
as children of the pre-created root element. This results in a valid DOM
without having to modify the stylesheet.

Regards,

Simon



>         ----- Original Message ----- 
>         From: Sean Leblanc
>         To: 'xalan-j-users@xml.apache.org'
>         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.
>         


Re: Converting DOM to text

Posted by Pae Choi <pa...@earthlink.net>.
Sometimes, I posted one including the code that does this job.
Search the archive.


Pae

  ----- Original Message ----- 
  From: Sean Leblanc 
  To: 'xalan-j-users@xml.apache.org' 
  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.


RE: Converting DOM to text

Posted by Gary L Peskin <ga...@firstech.com>.
See http://xml.apache.org/xalan-j/faq.html#faq-N10237

Change your 

 transformer.transform(xmlDomSource, domResult);

to

 transformer.transform(xmlDomSource, new StreamResult(System.out));

and I think you'll get what you're looking for.  You'll also see what's
causing the HIERARCHY_REQUEST_ERR.

HTH,
Gary

-----Original Message-----
From: Sean Leblanc [mailto:SeanL@e-infodata.com] 
Sent: Monday, January 20, 2003 10:05 AM
To: 'xalan-j-users@xml.apache.org'
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.