The pain of raw XML
Posted & filed under C++, GADfly.
It’s like the dark ages in here just now. Being so used to working in Java with XMLBeans I’ve been spoiled when it comes to XML handling. I never have to see it. OK, I have to design XML schemata now and then if they don’t exist but it means I can work on XML as domain objects. Now, working in C++ again, it’s back to raw XML. Here’s an example:
<user> <id>test</id> </user>
with Java and XMLBeans I could just do:
userDoc.getUser().getId().getValue();
with C++ I have to do a bit more work:
pDoc = parser->getDocument();
pRootNode = pDoc->getDocumentElement();
pNodes = pRootNode->getElementsByTagName(L"user");
for (int c=0; c < pNodes->getLength(); c++) {
pNode = pNodes->item(c);
pDomainObjectNodes = ((DOMElement*)(pNode))->getElementsByTagName(L"*");
for (int cc=0; cc < pDomainObjectNodes->getLength(); cc++) {
pDomainNode = pDomainObjectNodes->item(cc);
pTextNode = pDomainNode->getFirstChild();
printf("%ls -> %ls\n", pDomainNode->getNodeName(), pTextNode->getNodeValue());
}
}
not only that, look at the mess on the second last line of code:
pTextNode = pDomainNode->getFirstChild();
a Text Node of all things. Instead of the XML you see, this is actually the case:
<id><text>test</text></id>
I ask you! I knew that but when I first started working with XML it threw me. Why on earth? What the?