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 Gregory Freeland <gf...@jpg.com> on 2006/05/23 23:00:06 UTC
writing XML from a string
I've been using Xerces-C DOM and have run into an issue when writing to an
XML file. Basically, to write the XML file, I build up a DOMDocument,
adding nodes and such. Once the document is complete, the DOMWriter is used
to write the node to a file. I have cases though, where I'll have a string
containing XML characters, and several levels of descendant nodes.
Essentially, I want to write the string directly to the file as a child
node.
For example, the code snippet below will create a new DOM element, add an
attribute, and add a child text node. My problem occurs when the XMLCh*
variable "content" is a string that looks something like this:
"<Child>Test Content</Child>"
the resulting XML file will have the XML format characters translated.
<Child>Test Content</Child>
So is it possible to write the above XML string as a Text node, while
retaining the XML formatting?
Thanks in advance,
Greg
//////Begin source code
//first, create a new dataitem element
DOMElement* dataitemElem = m_DOMDocument->createElement(
X("Construction") );
//create and set the type attribute
dataitemElem->setAttribute(X("Type"), type);
//create a child text node of the new dataitem element
DOMText* dataitemElemVal = m_DOMDocument->createTextNode(content);
dataitemElem->appendChild((DOMNode*)dataitemElemVal);
//append the new child element to the root element
DOMElement* rootElem = m_DOMDocument->getDocumentElement();
rootElem->appendChild(dataitemsElem);
///////End source code
--
Re: writing XML from a string
Posted by David Bertoni <db...@apache.org>.
Gregory Freeland wrote:
> I've been using Xerces-C DOM and have run into an issue when writing to
> an XML file. Basically, to write the XML file, I build up a
> DOMDocument, adding nodes and such. Once the document is complete, the
> DOMWriter is used to write the node to a file. I have cases though,
> where I'll have a string containing XML characters, and several levels
> of descendant nodes. Essentially, I want to write the string directly to
> the file as a child node.
>
> For example, the code snippet below will create a new DOM element, add
> an attribute, and add a child text node. My problem occurs when the
> XMLCh* variable "content" is a string that looks something like this:
>
> "<Child>Test Content</Child>"
>
> the resulting XML file will have the XML format characters translated.
>
> <Child>Test Content</Child>
>
> So is it possible to write the above XML string as a Text node, while
> retaining the XML formatting?
>
You are confusing content with markup/structure. If you want to
serialize markup, create structure (elements and text nodes), not a
single text node.
You could also hack the serializer code, but then you are essentially
breaking the code to match your model, which is inherently broken.
Dave