c++ xml 解析器功能不起作用
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DOMElement
可能包含其他DOMElements
或DOMText
的集合。要获取元素的文本值,您需要调用方法getTextContent()
,getNodeValue
将始终返回NULL
。从概念上讲,这是另一种更好的方法,因为 DOMText 是 DOMElement 的子节点,我们可以遍历子节点并获取值。
以下是方法形式的逻辑:
希望这有帮助:)
A
DOMElement
may contain a collection of otherDOMElements
or aDOMText
. To get the text value of an element you need to call the methodgetTextContent()
,getNodeValue
will always returnNULL
.The is another better way conceptually, as the
DOMText
is a child of theDOMElement
we can traverse through the child node and get the value.Below is the logic in the form of a method:
Hope this helps :)
getNodeValue()
将始终返回空字符串,因为元素节点的“值”位于其子节点中。在我们的例子中,它是文本节点子节点。无论哪种方式都是迭代子节点或使用
getTextContent
。首先使用
hasChildNodes()
检查节点中的子节点,然后使用getFirstChild()
等方法。然后使用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 nodesor use
getTextContent
.First check for child nodes in a node using
hasChildNodes()
then use methods likegetFirstChild()
etc. . Afterwards usegetNodeValue()
.