Subjects
Home
VOTE Move XML Commons to Xerces
Commented: (XERCESJ 589) Bug with pattern restriction on long strings
: Xerces J 2 8 1 Release on Wednesday, September 13th
: Xerces J 2 9 0 Release on Wednesday, November 22nd
Commented: (XERCESJ 1066) Restriction+choice+substitutionGroup error
Commented: (XERCESJ 1178) Error getting prefix for an attribute with no n
Updated: (XERCESJ 1244) XMLSchemaValidator does not contribute element 's
Some consideration about the xerces DOM implementation
Updated: (XERCESJ 1066) Restriction+choice+substitutionGroup error
Commented: (XERCESJ 1227) Poor performance / OutOfMemoryError for sequenc
retain exception stack traces
Updated: (XERCESJ 1193) NPE or hang when parsing using the "continue afte
Future of NekoHTML
Commented: (XERCESJ 1203) NPE in XMLDTDProcessor
DOM Level 3 APIs for Xalan J and a new Xalan release (2 7 1)
: xml commons external 1 3 04 Release on Wednesday, November 22nd
Commented: (XERCESJ 1247) Incorrect location information on SAX when usin
XInclude exceptions how to mirror Xerces J functionality into Xerces C++?
First proposal on SoC project "Add support for the StAX (JSR 173) cursor API
: xml commons resolver 1 2 Release on Wednesday, November 22nd
Typo in RangeToken java Please check
Validator features
java lang ClassCastException when adopting Node
using the org apache xerces impl xs identity package
Updated: (XERCESJ 1257) buffer overflow in UTF8Reader for characters out
Problem with ref attributes and schema validation
Updated: (XERCESJ 122) XMLSchemaValidator does not contribute element 's d
Performance problem under load Xerces with Weblogic 9 x
remove ignored memory allocation
Commented: (XERCESJ 1177) SAXXMLStreamReader doesn 't always report namesp
Commented: (XERCESJ 977) Null pointer exception during DOM parsing
Commented: (XERCESJ 1197) Code cleanup for org apache xml serialize
Commented: (XERCESJ 1201) Initial contribution for StAX Event API
Updated: (XERCESJ 1061) Regex "$ " and "^ " characters treated as special c
Commented: (XERCESJ 1199) SAXXMLStreamReader should attempt to register a
Commented: (XERCESJ 1061) Regex "$ " and "^ " characters treated as special
Updated: (XERCESJ 589) Bug with pattern restriction on long strings
StackOverflow
xerces Range unnecessarily not garbage collectable if not detached
Updated: (XERCESJ 1178) Error getting prefix for an attribute with no nam
Bug in xs:redefine
Commented: (XERCESJ 1204) Can not set XMLEntityResolver for LSParser
Updated: (XERCESJ 1253) Prototype for SoC2007 project "Add support for th
Updated: (XERCESJ 1259) Add SteamFilter Function to SoC2007 project "Add
Assigned: (XERCESJ 444) SAXException thrown by EntityResolver is reported
Google Summer of Code 2007
Xerces J and XInclude relative path issue
Assigned: (XERCESJ 206) Stack overflow when using a schema validation
Commented: (XERCESJ 1215) Restrictions involving two levels of substituti
Closed: (XERCESJ 1203) NPE in XMLDTDProcessor
non overriding equals methoda
Resolved: (XERCESJ 1079) invalid value returned for TOTALDIGITS facet in
Xerces AS3 port
Updated: (XERCESJ 325) Regular Expression; Pattern "| " clause order de
Updated: (XERCESJ 1196) Javadoc generation fails on Java SE 5 0
Closed: (XERCESJ 1202) DTD validation on XIncluded documents when the sch
Created: (XERCESJ 1124) Nonspecific schema error message
a bug in xerces
Updated: (XERCESJ 1201) Initial contribution for StAX Event API
Closed: (XERCESJ 1254) Empty uris in targetNamespace attribute not report
Links
Home
Oracle database error code
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Problem In creating PI node using XMLSerializer

Problem In creating PI node using XMLSerializer

2003-07-21       - By peter peter
Reply:     1     2     3  


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&nbsp;created when I serialize the
XML using XMLSerializer. I want to insert the PI before root&nbsp;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 !&nbsp;&nbsp;&nbsp;&nbsp;</P>
<P>Here is the code I am using -- &gt;</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DocumentBuilderFactory factory <BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = DocumentBuilderFactory.newInstance();<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; factory.setNamespaceAware(true);<BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; DocumentBuilder builder = factory.newDocumentBuilder();<BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; DOMImplementation impl = builder.getDOMImplementation()
;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create
the document<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DocumentType svgDOCTYPE = impl
.createDocumentType(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "svg", "-//W3C//DTD
SVG 1.0//EN", <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<BR>&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp; Document doc = impl.createDocument(<BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; "<A
href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</A>", "svg",
svgDOCTYPE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; // Fill the document<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Node
rootElement = doc.getDocumentElement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ProcessingInstruction xmlstylesheet <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
doc.createProcessingInstruction("xml-stylesheet",<BR>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; "type=\"text/css\" href=\"standard.css\"");<BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; Comment comment = doc.createComment(<BR>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; "An example from Chapter 10 of Processing XML with Java");<BR>&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp; doc.insertBefore(comment, rootElement);<BR>&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp; doc.insertBefore(xmlstylesheet, rootElement);<BR>&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp; Node desc = doc.createElementNS(<BR>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; "<A
href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</A>", "desc");<BR
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rootElement.appendChild(desc);<BR>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; Text descText = doc.createTextNode(<BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; "An example from Processing XML with Java");<BR>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; desc.appendChild(descText);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<BR>&nbsp;&nbsp;&nbsp; try{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; OutputFormat format = new OutputFormat( doc ); //Serialize
DOM<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
format.setIndenting(true);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; StringWriter stringOut = new StringWriter();&nbsp; //Writer
will be a String<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp; XMLSerializer serial = new XMLSerializer( stringOut, format
);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
serial.asDOMSerializer();&nbsp;&nbsp;// As a DOM Serializer<BR>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serial.serialize( doc
.getDocumentElement());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch(Exception e)<BR>&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>
&nbsp; </P>
<P>But if I use Transformer for serialization , then ProcessingInstruction Node
is being created... using below code...</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TransformerFactory xformFactory <BR>&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = TransformerFactory.newInstance();&nbsp; <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Transformer idTransform = xformFactory
.newTransformer();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Source input = new
DOMSource(doc);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result output = new
StreamResult(System.out);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idTransform
.transform(input, output);&nbsp;&nbsp;&nbsp;&nbsp; <BR></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thank you very much,</P>
<P>&nbsp;</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.