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 "Vijay S. Bajwa" <vi...@citigroup.com> on 2007/01/05 16:31:16 UTC
DOMParsing question
Say I have a simple XML:
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2007 (http://www.altova.com)-->
<cnclMsg xsi:noNamespaceSchemaLocation="GTS-Oasys.xsd" xmlns:xsi="http://www.w3.
org/2001/XMLSchema-instance">
<msgHdr>
<dateTime>4294967295</dateTime>
<institution>a</institution>
<broker>a</broker>
<reference>100001</reference>
<version>4096</version>
<brkrIntRef>aaaaaaaaaaaaaaaa</brkrIntRef>
<instIntRef>aaaaaaaaaaaaaaaa</instIntRef>
</msgHdr>
<comment>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</comment>
</cnclMsg>
I have parsed it and created a DOM tree. No I want to walk it and extract the
data. I tried this general strategy. The trouble is that, for the msgHdr
element node, I am able to get the "name" and then, I guess I will parse out
this element. However, for the "comment" node, it does not give me any name,
just something like "text" or something like that. Is there any way to get the
name of a text node? The other alternative is to call getElementsByTagName() of
which I have heard bad things. Appreciate your helpful comments!
DOMElement* rootElmnt = doc->getDocumentElement() ;
DOMElement* tmp;
DBGOUT << "Root element: " << StrX(rootElmnt->getTagName()) << ENDL
for (child = rootElmnt->getFirstChild(); child != 0;
child=child->getNextSibling())
{
// equivalent to typeof check and static cast
if ( ! (tmp = dynamic_cast<DOMElement*>(child)) )
continue;
name = StrX(tmp->getTagName()).localForm() ;
DBGOUT << funcName << ": node name: " << name << ENDL
if (strcmp(name, msgHdr) == 0) {
get_msgHdr (tmp, &cncl->hdr, &cncl->trade_hdr);
}
else if (strcmp(name, comment) == 0) {
val = StrX(tmp->getNodeValue ()).localForm();
int len = MIN (OAS_COMMENT_SIZE, strlen(val) );
strncpy (cncl->comment, val, len);
}
}
Thanks!
Vijay
---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org