Im using  Xerces 1.2.1 & JDK 1.3

Here is my code ::

 


import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
import org.apache.xml.serialize.*;
import java.io.*;

 

public class DOMGenerator
{
 
 /**
  * default constructor
  */
 public DOMGenerator()
 {
  
 }
 
 
 
public String getDocumentAsString(Document doc)
{

 ByteArrayOutputStream str = new ByteArrayOutputStream();
 try
 {
  OutputFormat format = new OutputFormat();
  XMLSerializer serializer = new XMLSerializer(str, format);
  serializer.serialize(doc.getDocumentElement());
 }
 catch(Exception e)
 {
  }
 return str.toString();
}

 public Document parseString(String str)
 {
  Document doc = null;
  try
  {
   byte[] byte_array_of_string = str.getBytes();
   InputStream istr = new ByteArrayInputStream(byte_array_of_string);
   InputSource is = new InputSource(istr);
   DOMParser dom_parser = new DOMParser();
   dom_parser.parse(is);
   doc = dom_parser.getDocument();
  }
  catch(Exception e)
  {
   }
  return(doc);
 }
 
 public Document manipulateDOM(Document doc)
 {
  Element e  = doc.createElement("location");
  Text t = doc.createTextNode("a");
  e.appendChild(t);
  Element root = doc.getDocumentElement();
  root.appendChild(e);
  return(doc);
 }

 
 public static void main(String args[])
 {
  try
  {
   DOMGenerator dom_generator = new DOMGenerator();
   String s = "<?xml version='1.0'?><?xml-stylesheet type='text/xsl' href=''?><data><name>a</name><age>1</age></data>";


   Document doc = dom_generator.parseString(s);
   doc = dom_generator.manipulateDOM(doc);
   s = dom_generator.getDocumentAsString(doc);
   System.out.println(s); // this gives me <data><name>a</name><age>1</age><location>a</location></data> instead of <?xml version='1.0'?><?xml-stylesheet type='text/xsl' href=''?><data><name>a</name><age>1</age><location>a</location></data>
  }
  catch(Exception e)
  {}
 }
}

 Dave Flanagan <[EMAIL PROTECTED]> wrote:


Assuming you are using DocumentBuilder's parse method:
-assumption necessary since not too many details of your environment have been
specified

the parse method returns an object of type Document
which represents the entire XML document

What are the chances that you have called the getDocumentElement method
from the Document interface to return an object of type Element

and then serializing the Element node instead of the Document node
since the Element node does not include the stylesheet processing instruction
it would not be a part of the serialized output of XMLSerializer

I have run your example through Xerces serialize method in the XMLSerializer
and obtained the correct output you are expecting - no problems at all
and the OutputFormat object created was via the no args constructor

Then if the process is repeated but child node of the Document - the root
element of the document via the getDocumentElement method is passed as the
argument to the serialize method - the processing instruction does not appear
as would be expected as normal behaviour.

Hope this helps....
but your question is very vague in the lack of details...

Such as your statement
" I then manipulate the DOM "

also - the serializer you are using - are you setting any options in the
outputformat?

What version of Xerces are you using?

information pertaining to the above would be helpful
in providing you with answers to your problem,
as opposed to having to speculate on exactly what problem you are experiencing
and having to speculate upon the environment you are running in.

_________________________________________________________________________
This mail sent via toadmail.com, web e-mail @ ToadNet - want to go fast?
http://www.toadmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now

Reply via email to