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=""> 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.
