0

I am using XML DOM API in C++ to parse an XML file. I can't find any method to get the attribute value in a node element.

For example, in the following xml

<test>
<fruit count="10">
...
...
</fruit>
<test>

I need to get the count string("10") using XML APIs. Can anybody help me with some code snippets.

Chris Summers
  • 10,153
  • 1
  • 21
  • 46

2 Answers2

1

Use DOM Parser API to get attribute value count.

Refer below sample code:

//code to perform some process for parsing the input file and get rootElement

DOMNodeList* fruitNodes= rootElement->getElementsByTagName(XMLString::transcode("fruit"));

DOMNode* node = fruitNodes->item(0);

DOMElement* fruitElement = dynamic_cast <xercesc::DOMElement*>(node);

const XMLCh* attrValue = fruitElement->getAttribute(XMLString::transcode("count"));

you can get value 10 from attrValue using: string(XMLString::transcode(attrValue))

user2807083
  • 2,962
  • 4
  • 29
  • 37
  • Nice solution! Remember to use the built-in editor to format your code. This makes it easier for other users to find an read your code snippets. – Brian Hannay Jan 21 '21 at 07:44
0

Based on http://msdn.microsoft.com/en-us/library/windows/desktop/ms754523(v=vs.85).aspx

Try something like:

pXMLDomNodeList = pXMLDocElement->selectNodes("/test/fruit/@count");
Chris Summers
  • 10,153
  • 1
  • 21
  • 46