[xsd-users] Re: Setting element text content
Boris Kolpackov
boris at codesynthesis.com
Fri Jul 14 02:56:35 EDT 2006
Hi Matt,
Matt Burnham <mwburn at mhpcc.hpc.mil> writes:
> If I have the following schema:
>
> <xs:element name="stuff">
> <xs:complexType>
> <xs:attribute name="attr1" type="xs:string" use="required"/>
> </xs:complexType>
> </xs:element>
>
> And I'm generating the cxx-tree, in memory with the following code:
>
> stuff::attr1::type a1( "X" );
> stuff my_stuff( a1 );
> std::string data = "Text Goes Here";
> dom::auto_ptr<DOMDocument> dom_doc( stuff_(my_stuff, nsm) );
> XMLCh *xml_data = xercesc::XMLString::transcode( data.c_str() );
> dom_doc->setTextContent( xml_data);
> xercesc::XMLString::release( &xml_data );
>
> // serialize DOMDodument back to xml
>
> I get the following:
>
> <stuff attr1="X"/>
>
> How can I add "text content" to the element to generate the following xml:
>
> <stuff attr1="X">Text Goes Here</stuff>
Well, you realize that this document is not valid per your schema (e.g.,
if you try to parse it back with validation enabled, you will get a
parsing error). To set text content for a DOM element you will need to
call setTextContent on that node (as opposed to on the document node
as you've done above):
DOMElement* e (dom_doc->getDocumentElement ()); 'stuff'
e->setTextContent (xml_data);
Also note that this is a question about DOM rather than XSD which this
mailing list is for. I suggest for question like this you use Xerces-C++
documentation[1], examples[2] and mailing lists[3]. In particular,
the CreateDOMDocument example does exactly what you've asked.
[1] http://xml.apache.org/xerces-c/program.html
[2] http://xml.apache.org/xerces-c/samples.html
[3] http://xml.apache.org/xerces-c/feedback.html
hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060714/54df0d32/attachment.pgp
More information about the xsd-users
mailing list