You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Urs Baumeler <bm...@ten.ch> on 2000/05/23 12:18:45 UTC

How to re-build a XML document in Memory (long)

Hi

I've successfully created a program that parses an XML document
(using Xerces-C++) and stores the elements of the document in an
ORACLE DB. 

I'm now trying to rebuild the XML Tree in memory using the elements
from the DB and the DOM interface. The idea is to build an exact
copy of the document as is has been parsed in the first time.

Sadly, I couldn't find out how to do it correctly, even with the
help of the CreateDOMDocument sample. The first few elements of
a sample document are (debug output during parse run):

Node 1 Parent NONE Type 9
        ** node_type_nr     9
        ** node_name        #document
        ** node_value       <?xml version="1.0" encoding="ISO-8859-1"?>

Node 2 Parent 1 Type 7
        ** node_type_nr     7
        ** node_name        xml:stylesheet
        ** node_value       <?xml:stylesheet type="text/css"
href="../lgbl.css"?>

Node 3 Parent 1 Type 8
        ** node_type_nr     8
        ** node_name        #comment
        ** node_value       <!--?xml:stylesheet type="text/xsl"
href="lgbl.xsl"?-->

Node 4 Parent 1 Type 1
        ** Attribut 0 Name  aabbcc
        ** Attribut 0 Value abcdefghijklmnop
        ** node_type_nr     1
        ** node_name        ddeeff
        ** node_value 

My idea is to

- Create an empty document using createDocument()
- Adding elements using createElement() and appendChild()

How do I create the first (document) node with the correct name
and value? The createDocument function doesn't seem to give me 
the possibility to assign these values.

Does anybody have an example or a more detailed description on
how to create/rebuild an XML document in memory?

Thanks

Urs

Re: How to re-build a XML document in Memory (long)

Posted by Craig Noah <Cr...@sterling.com>.
I am working on an XML wrapper class, and have this scenario for my
default constructor and my addTag functions.  The document tag is never
found in the file - the contents of an XML file are "wrapped" in the
document tag.  So, my class stores two elements - the document element
(which is used to create nodes) and the top-level element (which is the
parent to all of the pertinent tags).  Thus, my constructor code looks
like this:
    try
    {
        XMLPlatformUtils::Initialize();
    }
    catch ( /* copy from the DOMPrint sample */ )
    {
    }
    DOM_DOMImplementation impl;
    doc = impl.createDocument(0, "Message", DOM_DocumentType());
    message = doc.getDocumentElement();

Of course, this code does not specify a DTD to be used with the DOM tree -
I don't have that perfected yet.  However, this code will create the start
of an XML document that can be passed through the parser (without
validation).  Then, to add a node such as
<subject>This is a test</subject> I use the following code:
    DOM_Element newElem = doc.createElement(tagName);
    message.appendChild(newElem);
    DOM_Text newElemVal = doc.createTextNode(tagValue);
    newElem.appendChild(newElemVal);

This allows an application to pass the name and value of a new element to
be created, and the wrapper class does the work of creating the element
node and the text node and appending them to the message tag (which is at
the top of the tree, as a child of the document node).

I hope this helps.

Craig Noah                  INTERNET: Craig.Noah@sterling.com
Software Engineer

Computer Associates
1404 Fort Crook Road South     Phone:    (402) 291-8300 x 284
Bellevue,  NE   68005-2969     FAX:      (402) 291-4362
Urs Baumeler wrote:

> Hi
>
> I've successfully created a program that parses an XML document
> (using Xerces-C++) and stores the elements of the document in an
> ORACLE DB.
>
> I'm now trying to rebuild the XML Tree in memory using the elements
> from the DB and the DOM interface. The idea is to build an exact
> copy of the document as is has been parsed in the first time.
>
> Sadly, I couldn't find out how to do it correctly, even with the
> help of the CreateDOMDocument sample. The first few elements of
> a sample document are (debug output during parse run):
>
> Node 1 Parent NONE Type 9
>         ** node_type_nr     9
>         ** node_name        #document
>         ** node_value       <?xml version="1.0" encoding="ISO-8859-1"?>
>
> Node 2 Parent 1 Type 7
>         ** node_type_nr     7
>         ** node_name        xml:stylesheet
>         ** node_value       <?xml:stylesheet type="text/css"
> href="../lgbl.css"?>
>
> Node 3 Parent 1 Type 8
>         ** node_type_nr     8
>         ** node_name        #comment
>         ** node_value       <!--?xml:stylesheet type="text/xsl"
> href="lgbl.xsl"?-->
>
> Node 4 Parent 1 Type 1
>         ** Attribut 0 Name  aabbcc
>         ** Attribut 0 Value abcdefghijklmnop
>         ** node_type_nr     1
>         ** node_name        ddeeff
>         ** node_value
>
> My idea is to
>
> - Create an empty document using createDocument()
> - Adding elements using createElement() and appendChild()
>
> How do I create the first (document) node with the correct name
> and value? The createDocument function doesn't seem to give me
> the possibility to assign these values.
>
> Does anybody have an example or a more detailed description on
> how to create/rebuild an XML document in memory?
>
> Thanks
>
> Urs
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org

--




Re: How to re-build a XML document in Memory (long)

Posted by Arundhati Bhowmick <ar...@hyperreal.org>.
There are some test cases like DomTest, DomMemTest, Traversal, etc under
tests/DOM which might give a fairly good idea of usage of DOM APIs. They
alos include the creation of DOM documents in memory.

Arundhati

Urs Baumeler wrote:

> Hi
>
> I've successfully created a program that parses an XML document
> (using Xerces-C++) and stores the elements of the document in an
> ORACLE DB.
>
> I'm now trying to rebuild the XML Tree in memory using the elements
> from the DB and the DOM interface. The idea is to build an exact
> copy of the document as is has been parsed in the first time.
>
> Sadly, I couldn't find out how to do it correctly, even with the
> help of the CreateDOMDocument sample. The first few elements of
> a sample document are (debug output during parse run):
>
> Node 1 Parent NONE Type 9
>         ** node_type_nr     9
>         ** node_name        #document
>         ** node_value       <?xml version="1.0" encoding="ISO-8859-1"?>
>
> Node 2 Parent 1 Type 7
>         ** node_type_nr     7
>         ** node_name        xml:stylesheet
>         ** node_value       <?xml:stylesheet type="text/css"
> href="../lgbl.css"?>
>
> Node 3 Parent 1 Type 8
>         ** node_type_nr     8
>         ** node_name        #comment
>         ** node_value       <!--?xml:stylesheet type="text/xsl"
> href="lgbl.xsl"?-->
>
> Node 4 Parent 1 Type 1
>         ** Attribut 0 Name  aabbcc
>         ** Attribut 0 Value abcdefghijklmnop
>         ** node_type_nr     1
>         ** node_name        ddeeff
>         ** node_value
>
> My idea is to
>
> - Create an empty document using createDocument()
> - Adding elements using createElement() and appendChild()
>
> How do I create the first (document) node with the correct name
> and value? The createDocument function doesn't seem to give me
> the possibility to assign these values.
>
> Does anybody have an example or a more detailed description on
> how to create/rebuild an XML document in memory?
>
> Thanks
>
> Urs
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: How to re-build a XML document in Memory (long)

Posted by Andy Heninger <an...@jtcsv.com>.

----- Original Message ----- 
From: "Urs Baumeler" <bm...@ten.ch>
To: <xe...@xml.apache.org>
Sent: Tuesday, May 23, 2000 3:18 AM
Subject: How to re-build a XML document in Memory (long)


> I'm now trying to rebuild the XML Tree in memory using the elements
> from the DB and the DOM interface. The idea is to build an exact
> copy of the document as is has been parsed in the first time.

Recreating the exact, character-for-character matching, original
document will not be possible.  The XML specs from W3C define an
"information set" of what is meaningful and what is not.  Some
stuff is lost, including the order of attributes in an element, 
white-space normalized out of attributes, and the exact form 
of new-lines.

> 
> Node 1 Parent NONE Type 9
>         ** node_type_nr     9
>         ** node_name        #document
>         ** node_value       <?xml version="1.0" encoding="ISO-8859-1"?>
> 

Document nodes are required to have a null value by the DOM spec.
The DOM recommendation does not provide a standard way to access the
XML declaration.  The latest Xerces sources (from the repository
or the daily-builds) includes a DOM_XMLDecl node type for this
purpose.  Since this is not yet standardized, it may change later,
if the W3C comes up with an alternative way of doing the same thing.


 Andy Heninger
IBM XML Technology Group, Cupertino, CA
heninger@us.ibm.com