Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XML DOM set node value (http://www.velocityreviews.com/forums/t170150-xml-dom-set-node-value.html)

keepyourstupidspam 11-08-2005 02:47 PM

XML DOM set node value
 
Hi,

I am using xerces dom C++, I want to change an element value, here is
the code I am using but the element in the file is not getting updated.

...
...
...
xercesc_2_4::DOMDocument* xmlDoc = parser->getDocument();
xercesc::DOMNodeList* list;
xercesc::DOMNode* node;

list = xmlDoc->getElementsByTagName( XMLString::transcode(Key.c_str())
);
node = list->item(0);
if( xercesc::DOMNode::ELEMENT_NODE == node->getNodeType() )
{
node->setNodeValue(XMLString::transcode(Value.c_str())) ;
}

Any help.


Thanks,
em


Martin Honnen 11-08-2005 02:57 PM

Re: XML DOM set node value
 


keepyourstupidspam wrote:


> list = xmlDoc->getElementsByTagName( XMLString::transcode(Key.c_str())
> );
> node = list->item(0);
> if( xercesc::DOMNode::ELEMENT_NODE == node->getNodeType() )
> {
> node->setNodeValue(XMLString::transcode(Value.c_str())) ;


In the DOM the node value of element nodes is supposed to be null and
you can't change that:
<http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>
It is not quite clear what you want to achieve, lots of people assume
the node value of an element node to be its text content thus if you
want to set the text content of the element then with DOM Level 3 (not
sure whether Xerces C supports that) you could do
node->setTextContent(XMLString::transcode(Value.c_str() ));
If you don't have DOM Level 3 then you need to remove all child nodes
and then add a new one with a text node, e.g. C++ pseudo code (not
tested/compiled!) could look alike
while (node->hasChildNodes()) {
node->removeChild(node->getLastChild());
}

node->appendChild(node->getOwnerDocument()->createTextNode(XMLString::transcode(Value.c_str() )));



--

Martin Honnen
http://JavaScript.FAQTs.com/

keepyourstupidspam 11-08-2005 03:39 PM

Re: XML DOM set node value
 
That didn't seem to work, but I will make it clearer what I am trying
to do.

Here is my XML

<MetaData>
<submissionID>12345</submissionID>
<SubmissionDate>Tue 11 08 15 33 05 2005</SubmissionDate>
<submissionType>Whatever</submissionType>
<description>Test Incident</description>
<currentState>Pending</currentState>
<annotations>Testing the data store manager</annotations>
<workflowID>0</workflowID>
<business>automobile</business>
<numAttachments>3</numAttachments>
<MetaData>

I just want ot change Pending to current in the <currentState> tag

em


Martin Honnen 11-08-2005 03:53 PM

Re: XML DOM set node value
 


keepyourstupidspam wrote:

> <currentState>Pending</currentState>


> I just want ot change Pending to current in the <currentState> tag


What I suggested (to remove all child nodes and insert a new text child
node) should work, as said the code was pseudo code as I don't use
Xerces C/C++ so you have to translate that into compilable C++ code
yourself.
If the element has exactly one text child node then it is easier, if you
have the element node then you can do
node->getFirstChild()->setNodeValue("current")
again pseudo code and you need to make sure you pass the Xerces DOM XML
string to setNodeValue and not a C string literal.
But the general approach described earlier is much safer as it works
whether that element has any child nodes or not or whether those child
nodes are text nodes or not.

There are also mailing list for users of specific Apache products thus
if you look at http://xml.apache.org/ for a Xerces C user mailing list
and post there then you are more likely to find someone to give you C++
code and not some generic DOM pseudo code.

--

Martin Honnen
http://JavaScript.FAQTs.com/


All times are GMT. The time now is 10:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.