You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Jens Sander <Ye...@ePost.de> on 2000/10/07 21:51:28 UTC
createProcessingInstruction problem
Hi !
I'v got a problem. I tried to modify the DOMGenerate.java sample
included in the xerces tar.gz.
I just wanted to add a single line concerning a processingInstruction.
Everywhere I looked for some more Information I found the
createProcessingInformation
method to be invoked on the document itself. But when running the
program the
output doesn't show the new ProcessingInstruction - line. I tried to
append this PI to
root. It worked. So why can't I append the PI to the document itself ?
So, have a look at the sourcecode :
>import org.w3c.dom.*;
>import org.apache.xerces.dom.DocumentImpl;
>import org.apache.xerces.dom.DOMImplementationImpl;
>import org.w3c.dom.Document;
>import org.apache.xml.serialize.OutputFormat;
>import org.apache.xml.serialize.Serializer;
>import org.apache.xml.serialize.SerializerFactory;
>import org.apache.xml.serialize.XMLSerializer;
>import java.io.*;
>public class DOMGenerate {
> public static void main( String[] argv) {
> try {
> Document doc= new DocumentImpl();
> doc.appendChild(doc.createProcessingInstruction("target",
"data"));
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Element root = doc.createElement("person"); // Create
Root Element
> Element item = doc.createElement("name"); // Create
element
> item.appendChild( doc.createTextNode("YenZi") );
> root.appendChild( item ); // atach
element to Root element
> item = doc.createElement("age"); // Create
another Element
> item.appendChild( doc.createTextNode("28" ) );
> root.appendChild( item ); // Attach
Element to previous element down tree
> item = doc.createElement("height");
> item.appendChild( doc.createTextNode("1.80" ) );
> root.appendChild( item ); // Attach
another Element - grandaugther
> doc.appendChild( root ); // Add Root
to Document
>
> Node node = doc.getFirstChild();
>
> PrintWriter out = res.getWriter();
> OutputFormat format = new OutputFormat( doc );
//Serialize DOM
> StringWriter stringOut = new StringWriter();
//Writer will be a String
> XMLSerializer serial = new XMLSerializer( stringOut,
format );
> serial.asDOMSerializer(); // As
a DOM Serializer
> // doku
> serial.serialize( doc.getDocumentElement() );
>
> out.println( "STRXML = " + stringOut.toString() ); //Spit
out DOM as a String
> } catch ( Exception ex ) {
> ex.printStackTrace();
> }
> }
>}
Can somebody help me with this problem ?
Im using Xerces-J-1.1.3 and SUN JDK1.2.2 under Debian Linux 2.2
(potato).
Thanx, YenZi