Actually, if you're using or overwise want to download the Xerces-C
distribution, there is a program in the "samples" directory called
"DOMPrint.cpp" that is exactly like your program below. Guess there isn't
anything like it currently built into Xerces...
-- HSN
On Wed, 9 Feb 2000, Kenneth Ramirez wrote:
>
>
> I convinced the organization that I'm currently consulting to start using
> Xerces
> in our middle tier to return data retrieved from the database to the browser.
> We actually have to first go through an ASP page before hitting the browser
> (since the site is managed with IIS). In order to return the XML document to
> the active server page, we're placing our data first into a DOM so that it
> creates the structure for us, then we pull out the XML document as a string
> and
> return it to the ASP. Here is the problem, I could not find a way to retrieve
> the code from the XML document as a string, so I had to write my own. Am I
> missing something, or am I correct in assuming that there is no way of pulling
> the data out from the DOM as a string? I'd expect that there would be some
> method on the DOM as follows:
>
> Document doc = ...; // Get the document and fill it with the XML data.
> String xmlDocAsString = doc.getXMLDocAsString();
> return xmlDocAsString;
>
> I went ahead and wrote my own, based on an example that I found on the IBM
> development site in the Java/XML tutorial. The following is the code that I
> wrote, which I hope is included into the Xerces implementation if the
> functionality is not there yet. I'm sure that others will have a similar
> need:
>
> String getXMLDocAsString(Node node)
> {
> String str = new String();
>
> int type = node.getNodeType();
> switch (type)
> {
> case Node.DOCUMENT_NODE:
> {
> str = str.concat("<?xml version=\"1.0\" ?>");
> str =
> str.concat(getXMLDocAsString(((Document)node).getDocumentElement()));
> break;
> }
>
> case Node.ELEMENT_NODE:
> {
> str = str.concat("<");
> str = str.concat(node.getNodeName());
> NamedNodeMap attrs = node.getAttributes();
> for (int i = 0; i < attrs.getLength(); i++)
> {
> Node attr = attrs.item(i);
> str = str.concat(" " + attr.getNodeName() + "=\"" +
> attr.getNodeValue() + "\"");
> }
> str = str.concat(">");
>
> NodeList children = node.getChildNodes();
> if (children != null)
> {
> int len = children.getLength();
> for (int i = 0; i < len; i++)
> str = str.concat(getXMLDocAsString(children.item(i)));
> }
>
> break;
> }
>
> case Node.ENTITY_REFERENCE_NODE:
> {
> str = str.concat("&");
> str = str.concat(node.getNodeName());
> str = str.concat(";");
> break;
> }
>
> case Node.CDATA_SECTION_NODE:
> {
> str = str.concat("<![CDATA[");
> str = str.concat(node.getNodeValue());
> str = str.concat("]]>");
> break;
> }
>
> case Node.TEXT_NODE:
> {
> str = str.concat(node.getNodeValue());
> break;
> }
>
> case Node.PROCESSING_INSTRUCTION_NODE:
> {
> str = str.concat("<?");
> str = str.concat(node.getNodeName());
> String data = node.getNodeValue();
> {
> str = str.concat(" ");
> str = str.concat(data);
> }
> str = str.concat("?>");
> break;
> }
> }
>
> if (type == Node.ELEMENT_NODE)
> {
> str = str.concat("</");
> str = str.concat(node.getNodeName());
> str = str.concat(">");
> }
>
> return str;
> }
>
> Thanks,
>
> Ken Ramirez (Master-Mind Consulting Services, Inc. - [EMAIL PROTECTED] or
> www.mastermind.com)
>
>
>
>