XML DOM set node value

Discussion in 'XML' started by keepyourstupidspam, Nov 8, 2005.

  1. 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
    keepyourstupidspam, Nov 8, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Nov 8, 2005
    #2
    1. Advertising

  3. 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
    keepyourstupidspam, Nov 8, 2005
    #3
  4. 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/
    Martin Honnen, Nov 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,396
  2. Replies:
    0
    Views:
    361
  3. Replies:
    5
    Views:
    733
    Alan Kennedy
    Nov 4, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,399
    Dimitre Novatchev
    Aug 24, 2006
  5. Replies:
    0
    Views:
    509
Loading...

Share This Page