You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Tristan Mehamli <tr...@tcelectronic.com> on 2005/06/10 11:13:39 UTC

XML generation

Hi,
 I want to create a DOM tree in order to generate an XML file with 
DOMWriter.

This is the an example of the document I want to generate:
<?xml version="1.0" Encoding="UTF-8" ?>
<!-- A comment-->
  <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="myschema.xsd">
     <tag1>
       <tag2>its value</tag2>
       <tag3 atName="value" atName="value"></tag3>
       <tag4>
         <tag5>
           <tag6>its value</tag6>
         </tag5>
       </tag4>
     </tag1>
</root>

Can someone give me an example of code to generate a DOM tree that 
describes this document?

Is there a way to generate a DOM tree from an XML schema and just fill 
its nodes?

Thanks for all answers.

--
Tristan Mehamli
Software Engineer
TC Electronic A/S

Re: XML generation

Posted by Tristan Mehamli <tr...@tcelectronic.com>.
Axel Weiß a écrit :

>Tristan Mehamli wrote:
>  
>
>>Hi,
>> I want to create a DOM tree in order to generate an XML file with
>>DOMWriter.
>>
>>This is the an example of the document I want to generate:
>><?xml version="1.0" Encoding="UTF-8" ?>
>><!-- A comment-->
>>  <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>     xsi:noNamespaceSchemaLocation="myschema.xsd">
>>     <tag1>
>>       <tag2>its value</tag2>
>>       <tag3 atName="value" atName="value"></tag3>
>>       <tag4>
>>         <tag5>
>>           <tag6>its value</tag6>
>>         </tag5>
>>       </tag4>
>>     </tag1>
>></root>
>>
>>Can someone give me an example of code to generate a DOM tree that
>>describes this document?
>>    
>>
>
>Hi Tristan,
>
>you first generate an empty document with 
>DOMImplementation::createDocument(). Subsequently you fill the root 
>element with children, according to your needs. Finally, you write out 
>the result DOMDocument with DOMWriter::writeToString().
>
>Read the docs! http://xml.apache.org/xerces-c/apiDocs/index.html is a 
>good starting point.
>
>  
>
>>Is there a way to generate a DOM tree from an XML schema and just fill
>>its nodes?
>>    
>>
>
>No.
>
>Hope it helps,
>			Axel
>
>  
>
Hi Axel,
Thanks for your answer. In fact I managed to do something by my own (may 
be not really efficient). Here is the code for those who are interested:

  DOMImplementation* impl = 
DOMImplementationRegistry::getDOMImplementation(X("Core"));

  DOMDocument* doc = impl->createDocument(
                      0,                    // root element namespace URI.
                      XStr("root"),    // root element name
                      0);                   // document type object (DTD).

  doc->setEncoding(XStr("UTF-8"));
  doc->setVersion(XStr("1.0"));
  DOMElement* rootElem = doc->getDocumentElement();
  rootElem->setAttribute(XStr("xmlns:xsi"), 
XStr("http://www.w3.org/2001/XMLSchema-instance"));
  rootElem->setAttribute(XStr("xsi:noNamespaceSchemaLocation"), 
XStr("aSchema.xsd"));

  DOMElement*  tag1 = doc->createElement(XStr("tag1"));
  rootElem->appendChild(tag1);

  DOMElement*  tag2 = doc->createElement(XStr("tag2"));
  tag1->appendChild(tag2 );
  tag2->setAttribute(XStr("index"),XStr("0"));
  DOMText*    tag2Val = doc->createTextNode(XStr("VALUE"));
  tag2->appendChild(tag2Val );

Yet I have one more question. Where can I find a description of the 
features that can be set when I create a new document (I took a look at 
the documentation but I probably missed something)?

Re: XML generation

Posted by Axel Weiß <aw...@informatik.hu-berlin.de>.
Tristan Mehamli wrote:
> Hi,
>  I want to create a DOM tree in order to generate an XML file with
> DOMWriter.
>
> This is the an example of the document I want to generate:
> <?xml version="1.0" Encoding="UTF-8" ?>
> <!-- A comment-->
>   <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>      xsi:noNamespaceSchemaLocation="myschema.xsd">
>      <tag1>
>        <tag2>its value</tag2>
>        <tag3 atName="value" atName="value"></tag3>
>        <tag4>
>          <tag5>
>            <tag6>its value</tag6>
>          </tag5>
>        </tag4>
>      </tag1>
> </root>
>
> Can someone give me an example of code to generate a DOM tree that
> describes this document?

Hi Tristan,

you first generate an empty document with 
DOMImplementation::createDocument(). Subsequently you fill the root 
element with children, according to your needs. Finally, you write out 
the result DOMDocument with DOMWriter::writeToString().

Read the docs! http://xml.apache.org/xerces-c/apiDocs/index.html is a 
good starting point.

> Is there a way to generate a DOM tree from an XML schema and just fill
> its nodes?

No.

Hope it helps,
			Axel

-- 
Humboldt-Universität zu Berlin
Institut für Informatik
Signalverarbeitung und Mustererkennung
Dipl.-Inf. Axel Weiß
Rudower Chaussee 25
12489 Berlin-Adlershof
+49-30-2093-3050
** www.freesp.de **