xerces-j-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gude Reshma <reshma_g...@yahoo.com>
Subject RE: Document Obj to a String/Stream?
Date Sat, 17 Aug 2002 04:35:52 GMT

which is the recommended way of converting document to String? using serializer or using the
meothd given below?
what i mean is what is the advantage of using this method & not serializer?
 "Soosai, Lourdhu Vasanth" wrote:hi ,
try to walk thru the dom,something like this


public class XMLDocumentWriter {
PrintWriter out; // the stream to send output to
}

public void write(Node node, String indent) {

switch(node.getNodeType()) {

case Node.DOCUMENT_NODE: 

{

Document doc = (Document)node;
out.println(indent + ""); 
Node child = doc.getFirstChild(); 
while(child != null) { 
write(child, indent); 
child = child.getNextSibling(); 

}
break; 
}

case Node.DOCUMENT_TYPE_NODE: 
{ 

DocumentType doctype = (DocumentType) node;
out.println("");
break; 
}

case Node.ELEMENT_NODE: 
{ 
Element elt = (Element) node;
out.print(indent + "<" + elt.getTagName()); 
NamedNodeMap attrs = elt.getAttributes(); 
for(int i = 0; i < attrs.getLength(); i++) { 
Node a = attrs.item(i);
out.print(" " + a.getNodeName() + "='" + 
fixup(a.getNodeValue()) + "'");
}
out.println(">"); 
String newindent = indent + " "; 
Node child = elt.getFirstChild(); 
while(child != null) { 
write(child, newindent); 
child = child.getNextSibling(); 
}
out.println(indent + " elt.getTagName() + ">"); 
break; 
}

case Node.TEXT_NODE: 
{ 
Text textNode = (Text)node;
String text = textNode.getData().trim(); 
if ((text != null) && text.length() > 0) 
out.println(indent + fixup(text)); 
break; 

}

case Node.PROCESSING_INSTRUCTION_NODE: 
{ 
ProcessingInstruction pi = (ProcessingInstruction)node;
out.println(indent + " " " + pi.getData() + "?>"); break;
}

case Node.ENTITY_REFERENCE_NODE: 
{ 
out.println(indent + "&" + node.getNodeName() + ";");
break; 

}

case Node.CDATA_SECTION_NODE: 
{ 
CDATASection cdata = (CDATASection)node;

out.println(indent + "<" + "![CDATA[" + cdata.getData() +
"]]" + ">"); break; }

case Node.COMMENT_NODE: 
{ 
Comment c = (Comment)node;
out.println(indent + "");
break; 

}
default: 
System.err.println("Ignoring node: " +
node.getClass().getName());
break;
}


}
String fixup(String s) { 
StringBuffer sb = new StringBuffer();
int len = s.length(); 
for(int i = 0; i < len; i++) {

char c = s.charAt(i);
switch(c) 
{

default: sb.append(c); break;

case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '&': sb.append("&"); break;
case '"': sb.append("""); break;
case '\'': sb.append("&apos;"); break;
} 

}

return sb.toString();


}

rgds
vasanth
-----Original Message-----
From: Kevin [mailto:kevinz00@yahoo.com]
Sent: Wednesday, August 14, 2002 6:41 PM
To: xerces-j-user@xml.apache.org
Subject: Document Obj to a String/Stream?


Hi, anyone know how to take a Document object and
output a corresponding XML String/Stream.
In JDOM, they have a XMLOutputter can do that:
==========================================================
public void output(Document doc, java.io.OutputStream
out)
throws java.io.IOException
This will print the Document to the given output
stream. The
characters are printed using the encoding specified in
the
constructor, or a default of UTF-8.
==========================================================
Does anyone know how to do this using Xerces or Xalan
without using JDOM?

Thanks,



__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org

This e-mail, including attachments, is intended for the exclusive use of the
person or entity to which it is addressed and may contain confidential or
privileged information. If the reader of this e-mail is not the intended
recipient or his or her authorized agent, the reader is hereby notified that
any dissemination, distribution or copying of this e-mail is prohibited. If
you think that you have received this e-mail in error, please advise the
sender by reply e-mail of the error and then delete this e-mail immediately.
Thank you. Aetna Inc.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org



---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs
Mime
View raw message