You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Jairam, Roopnaraine" <Ro...@ca.com> on 2004/12/17 23:11:15 UTC

setNodeValue() not working

Hi:

 

I trying to read and write to my xml file. I am able to read from the
file but when trying to update an element no errors are returned but the
file is not updated I have attached my code for the write please help

 

<LicenseSystem>

            <ContactInfo>

                        <FirstName>abc</FirstName>

                    <LastName>def</LastName>

                    <Address1>my address</Address1>

                    <Address2></Address2>

                    <City>abc</City>

                        <State>new york</State>

                        <Zip1>12122</Zip1>

                        <Zip2></Zip2>

                        <Country>usa</Country>

                        <Phone></Phone>

                        <Email>abc@aol.com</Email>

            </ContactInfo>

            

....

<LicenseSystem>

 

 

 

DWORD XML::parseFile() 

{

            if (m_parsed)

                        return ERROR_SUCCESS;

            if (m_parser != NULL)

            {

                        m_parser->parse(m_xmlFileName);

                        m_document = m_parser->getDocument();

                        if (m_document != NULL)

                        {

                                    m_parsed = true;

                                    return ERROR_SUCCESS;

                        }

            }

 

    return ERROR_INVALID_HANDLE;

}

 

DWORD XML::WriteUserInfo(PUSERINFO pUserInfo)

{

            if (pUserInfo == NULL)

                        return ERROR_NO_DATA;

 

            if (parseFile() != ERROR_SUCCESS)

                        return ERROR_INVALID_HANDLE;

 

            DOMNodeList* contactInfo =
m_document->getElementsByTagName(L"ContactInfo");

 

            if (contactInfo == NULL)

                        return ERROR_INVALID_HANDLE;

 

            DOMElement* element = (DOMElement *)contactInfo->item( 0 );

 

            SetElementText(element, L"FirstName",
pUserInfo->szFirstName);

            SetElementText(element, L" LastName",
pUserInfo->szLastName);

             ...

            return ERROR_SUCCESS;

}

 

 

// -----------------------------------------------

DWORD XML::SetElementText(DOMElement *pElem,  LPCWSTR pzName,  LPCSTR
value)

{

    if( pElem != NULL )

    {

        DOMNodeList *pList = pElem->getElementsByTagName(pzName);

        if( pList->getLength() > 0 )

        {

            DOMElement *pElem = (DOMElement *)pList->item( 0 );

            DOMNode *pChild = pElem->getFirstChild();

 

            if( pChild )

            {

 
pChild->setNodeValue(XMLString::transcode(value));

                        return ERROR_SUCCESS;

            }

        }

    }

 

            

   return ERROR_INVALID_HANDLE;

}

 

Thanks vince.


Re: setNodeValue() not working

Posted by Alberto Massari <am...@progress.com>.
Hi Vince,
from a cursory look to your code, the new values should update the 
FirstName and LastName content (btw, you have a memory leak by using 
XMLString::transcode without releasing the returned string).
One thing looks strange: you say "the file is not updated", but I don't see 
any code serializing the DOM back to disk. I hope you have written such 
code (otherwise the changes stay in memory): can you post it?

Alberto

At 17.11 17/12/2004 -0500, Jairam, Roopnaraine wrote:
>Hi:
>
>I trying to read and write to my xml file. I am able to read from the file 
>but when trying to update an element no errors are returned but the file 
>is not updated I have attached my code for the write please help
>
><LicenseSystem>
>             <ContactInfo>
>                         <FirstName>abc</FirstName>
>                     <LastName>def</LastName>
>                     <Address1>my address</Address1>
>                     <Address2></Address2>
>                     <City>abc</City>
>                         <State>new york</State>
>                         <Zip1>12122</Zip1>
>                         <Zip2></Zip2>
>                         <Country>usa</Country>
>                         <Phone></Phone>
>                         <Email>abc@aol.com</Email>
>             </ContactInfo>
>
>….
><LicenseSystem>
>
>
>
>DWORD XML::parseFile()
>{
>             if (m_parsed)
>                         return ERROR_SUCCESS;
>             if (m_parser != NULL)
>             {
>                         m_parser->parse(m_xmlFileName);
>                         m_document = m_parser->getDocument();
>                         if (m_document != NULL)
>                         {
>                                     m_parsed = true;
>                                     return ERROR_SUCCESS;
>                         }
>             }
>
>     return ERROR_INVALID_HANDLE;
>}
>
>DWORD XML::WriteUserInfo(PUSERINFO pUserInfo)
>{
>             if (pUserInfo == NULL)
>                         return ERROR_NO_DATA;
>
>             if (parseFile() != ERROR_SUCCESS)
>                         return ERROR_INVALID_HANDLE;
>
>             DOMNodeList* contactInfo = 
> m_document->getElementsByTagName(L”ContactInfo”);
>
>             if (contactInfo == NULL)
>                         return ERROR_INVALID_HANDLE;
>
>             DOMElement* element = (DOMElement *)contactInfo->item( 0 );
>
>             SetElementText(element, L“FirstName”, pUserInfo->szFirstName);
>             SetElementText(element, L” LastName”, pUserInfo->szLastName);
>              …
>             return ERROR_SUCCESS;
>}
>
>
>// -----------------------------------------------
>DWORD XML::SetElementText(DOMElement *pElem,  LPCWSTR pzName,  LPCSTR  value)
>{
>     if( pElem != NULL )
>     {
>         DOMNodeList *pList = pElem->getElementsByTagName(pzName);
>         if( pList->getLength() > 0 )
>         {
>             DOMElement *pElem = (DOMElement *)pList->item( 0 );
>             DOMNode *pChild = pElem->getFirstChild();
>
>             if( pChild )
>             {
>                         pChild->setNodeValue(XMLString::transcode(value));
>                         return ERROR_SUCCESS;
>             }
>         }
>     }
>
>
>    return ERROR_INVALID_HANDLE;
>}
>
>Thanks vince.



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org