c++ xml 解析器功能不起作用

发布于 2024-10-30 09:41:08 字数 1082 浏览 3 评论 0原文

我正在使用 xerces c++ 来操作 xml 文件?但 getNodeValue() 和 setNodeValue() 不起作用,但 getNodeName() 起作用。有人有什么建议吗?

 if( currentNode->getNodeType() &&  currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) 
         {
        // Found node which is an Element. Re-cast node as element
            DOMElement* currentElement= dynamic_cast< xercesc::DOMElement* >( currentNode );
            if( XMLString::equals(currentElement->getTagName(), TAG_ApplicationSettings))
            {
               // Already tested node as type element and of name "ApplicationSettings".
               // Read attributes of element "ApplicationSettings".
               const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
               m_OptionA = XMLString::transcode(xmlch_OptionA);
                   XMLCh* t,*s;
        //s= XMLString::transcode("manish");
        //currentNode->setElementText(s);
                  t=(XMLCh*)currentNode->getNodeName();
               s=(XMLCh*)currentNode->getNodeValue();

cout

I am using xerces c++ to manipulate an xml file? but getNodeValue() and setNodeValue() are not working but getNodeName() is working. Do anyone has any suggestions?

 if( currentNode->getNodeType() &&  currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) 
         {
        // Found node which is an Element. Re-cast node as element
            DOMElement* currentElement= dynamic_cast< xercesc::DOMElement* >( currentNode );
            if( XMLString::equals(currentElement->getTagName(), TAG_ApplicationSettings))
            {
               // Already tested node as type element and of name "ApplicationSettings".
               // Read attributes of element "ApplicationSettings".
               const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
               m_OptionA = XMLString::transcode(xmlch_OptionA);
                   XMLCh* t,*s;
        //s= XMLString::transcode("manish");
        //currentNode->setElementText(s);
                  t=(XMLCh*)currentNode->getNodeName();
               s=(XMLCh*)currentNode->getNodeValue();

cout<getNodeValue()) << "\n";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-11-06 09:41:08

DOMElement 可能包含其他 DOMElementsDOMText 的集合。要获取元素的文本值,您需要调用方法 getTextContent()getNodeValue 将始终返回 NULL
从概念上讲,这是另一种更好的方法,因为 DOMText 是 DOMElement 的子节点,我们可以遍历子节点并获取值。

以下是方法形式的逻辑:

string getElementValue(const DOMElement& parent)
{    
 DOMNode *child;   

 string strVal;

 for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())    
 {    
    if(DOMNode::TEXT_NODE == child->getNodeType())    
    {    
     DOMText* data = dynamic_cast<DOMText*>(child);    
     const XMLCh* val = data->getWholeText();    
     strVal += XMLString::transcode(val);    
    }    
    else    
    {    
        throw "ERROR : Non Text Node";    
    }    
 }    
 return strVal;   
}

希望这有帮助:)

A DOMElement may contain a collection of other DOMElements or a DOMText. To get the text value of an element you need to call the method getTextContent(), getNodeValue will always return NULL.
The is another better way conceptually, as the DOMText is a child of the DOMElement we can traverse through the child node and get the value.

Below is the logic in the form of a method:

string getElementValue(const DOMElement& parent)
{    
 DOMNode *child;   

 string strVal;

 for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())    
 {    
    if(DOMNode::TEXT_NODE == child->getNodeType())    
    {    
     DOMText* data = dynamic_cast<DOMText*>(child);    
     const XMLCh* val = data->getWholeText();    
     strVal += XMLString::transcode(val);    
    }    
    else    
    {    
        throw "ERROR : Non Text Node";    
    }    
 }    
 return strVal;   
}

Hope this helps :)

落日海湾 2024-11-06 09:41:08

getNodeValue() 将始终返回空字符串,因为元素节点的“值”位于其子节点中。在我们的例子中,它是文本节点子节点。无论哪种方式都是迭代子节点
或使用getTextContent
首先使用 hasChildNodes() 检查节点中的子节点,然后使用 getFirstChild() 等方法。然后使用getNodeValue()

DOMNode* ptrDomNode = SomeNode;

if(ptrDomNode->hasChildNodes())
{
    DOMNode* dTextNode =  ptrDomNode->getFirstChild();         
    char* string = XMLString::transcode(dTextNode->getNodeValue());
}

getNodeValue() will always return an empty string, because the "value" of an element node is in its child. In our case it is text node child. Either way is to iterate through child nodes
or use getTextContent.
First check for child nodes in a node using hasChildNodes() then use methods like getFirstChild() etc. . Afterwards use getNodeValue().

DOMNode* ptrDomNode = SomeNode;

if(ptrDomNode->hasChildNodes())
{
    DOMNode* dTextNode =  ptrDomNode->getFirstChild();         
    char* string = XMLString::transcode(dTextNode->getNodeValue());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文