Problem In creating PI node using XMLSerializer 2003-07-21 - By peter peter
Hi All,
I apolosize if this question has been asked before!
ProcessingInstruction node is not being created when I serialize the XML using XMLSerializer. I want to insert the PI before root Element, I tried by appending under the root element, it worked, but I want to create the PI node before the root Element,
Also if i use Transformer instead of XMLSerializer , then PI is created before root Element ( which is what I want). But I don't want to use Transformer !
Here is the code I am using -- >
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); // Create the document DocumentType svgDOCTYPE = impl.createDocumentType( "svg", "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" ); Document doc = impl.createDocument( "http://www.w3.org/2000/svg", "svg", svgDOCTYPE); // Fill the document Node rootElement = doc.getDocumentElement(); ProcessingInstruction xmlstylesheet = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"standard.css\""); Comment comment = doc.createComment( "An example from Chapter 10 of Processing XML with Java"); doc.insertBefore(comment, rootElement); doc.insertBefore(xmlstylesheet, rootElement); Node desc = doc.createElementNS( "http://www.w3.org/2000/svg", "desc"); rootElement.appendChild(desc); Text descText = doc.createTextNode( "An example from Processing XML with Java"); desc.appendChild(descText); try{ OutputFormat format = new OutputFormat( doc ); //Serialize DOM format.setIndenting(true); StringWriter stringOut = new StringWriter(); //Writer will be a String XMLSerializer serial = new XMLSerializer( stringOut, format ); serial.asDOMSerializer(); // As a DOM Serializer serial.serialize( doc.getDocumentElement()); } catch(Exception e) { e.printStackTrace(); }
But if I use Transformer for serialization , then ProcessingInstruction Node is being created... using below code...
TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransform = xformFactory.newTransformer(); Source input = new DOMSource(doc); Result output = new StreamResult(System.out); idTransform.transform(input, output);
Thank you very much,
--------------------------------- Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. <P>Hi All,</P> <P>I apolosize if this question has been asked before!</P> <P>ProcessingInstruction node is not being created when I serialize the XML using XMLSerializer. I want to insert the PI before root Element, I tried by appending under the root element, it worked, but I want to create the PI node before the root Element,</P> <P>Also if i use Transformer instead of XMLSerializer , then PI is created before root Element ( which is what I want). But I don't want to use Transformer ! </P> <P>Here is the code I am using -- ></P> <P> DocumentBuilderFactory factory <BR> = DocumentBuilderFactory.newInstance();<BR> factory.setNamespaceAware(true);<BR> DocumentBuilder builder = factory.newDocumentBuilder();<BR> DOMImplementation impl = builder.getDOMImplementation() ;<BR> <BR> // Create the document<BR> DocumentType svgDOCTYPE = impl .createDocumentType(<BR> "svg", "-//W3C//DTD SVG 1.0//EN", <BR> "<A href="http://www.w3 .org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">http://www.w3.org/TR/2001/REC-SVG -20010904/DTD/svg10.dtd</A>"<BR> );<BR>   ; Document doc = impl.createDocument(<BR> "<A href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</A>", "svg", svgDOCTYPE);<BR> <BR> // Fill the document<BR> Node rootElement = doc.getDocumentElement();<BR> ProcessingInstruction xmlstylesheet <BR> = doc.createProcessingInstruction("xml-stylesheet",<BR> "type=\"text/css\" href=\"standard.css\"");<BR> Comment comment = doc.createComment(<BR> "An example from Chapter 10 of Processing XML with Java");<BR>  ; doc.insertBefore(comment, rootElement);<BR>   ; doc.insertBefore(xmlstylesheet, rootElement);<BR>   ; Node desc = doc.createElementNS(<BR> "<A href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</A>", "desc");<BR > rootElement.appendChild(desc);<BR> Text descText = doc.createTextNode(<BR> "An example from Processing XML with Java");<BR> desc.appendChild(descText);<BR> <BR> try{<BR> OutputFormat format = new OutputFormat( doc ); //Serialize DOM<BR> format.setIndenting(true);<BR> StringWriter stringOut = new StringWriter(); //Writer will be a String<BR>   ; XMLSerializer serial = new XMLSerializer( stringOut, format );<BR> serial.asDOMSerializer(); // As a DOM Serializer<BR> serial.serialize( doc .getDocumentElement());<BR> }<BR>  ; catch(Exception e)<BR> {<BR> e.printStackTrace();<BR> }<BR> </P> <P>But if I use Transformer for serialization , then ProcessingInstruction Node is being created... using below code...</P> <P> TransformerFactory xformFactory <BR> = TransformerFactory.newInstance(); <BR> Transformer idTransform = xformFactory .newTransformer();<BR> Source input = new DOMSource(doc);<BR> Result output = new StreamResult(System.out);<BR> idTransform .transform(input, output); <BR></P> <P> Thank you very much,</P> <P> </P> <DIV></DIV><p><hr SIZE=1> Do you Yahoo!?<br> <a href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The New Yahoo! Search</a> - Faster. Easier. Bingo.
|
|