XML DOM set node value

G

Guest

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
 
M

Martin Honnen

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())));
 
G

Guest

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
 
M

Martin Honnen

keepyourstupidspam said:
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top