Node.hasChildNodesto find out if there are child elements. But this function returns TRUE even if there is only text within the node. That means every time I reach the bottom / lowermost level of the tree and there is only text content within this element, hasChildNodes returns TRUE and I get an error message. It says that there is no instance of an automation variable because the current node is not filled due to the fact that the current node list can't return a node from a text content. Or did I misunderstand something?
IF NOT ISCLEAR(XMLDocument) THEN CLEAR(XMLDocument); CREATE(XMLDocument); XMLDocument.setProperty('ProhibitDTD',FALSE); XMLDocument.validateOnParse(FALSE); IF XMLDocument.load('C:\temp3.xml') THEN BEGIN XMLCurrNode := XMLDocument.selectSingleNode(XMLDocument.documentElement.nodeName); IF XMLCurrNode.hasChildNodes THEN BEGIN XMLNodeList := XMLCurrNode.childNodes; XMLCurrNode := XMLNodeList.nextNode; MESSAGE('Node: %1\Text: %2',XMLCurrNode.nodeName,XMLCurrNode.text); IF XMLCurrNode.hasChildNodes THEN BEGIN XMLNodeList := XMLCurrNode.childNodes; XMLCurrNode := XMLNodeList.nextNode; MESSAGE('Node: %1\Text: %2',XMLCurrNode.nodeName,XMLCurrNode.text); IF XMLCurrNode.hasChildNodes THEN BEGIN XMLNodeList := XMLCurrNode.childNodes; XMLCurrNode := XMLNodeList.nextNode; MESSAGE('Node: %1\Text: %2',XMLCurrNode.nodeName,XMLCurrNode.text); IF XMLCurrNode.hasChildNodes THEN BEGIN XMLNodeList := XMLCurrNode.childNodes; XMLCurrNode := XMLNodeList.nextNode; MESSAGE('Node: %1\Text: %2',XMLCurrNode.nodeName,XMLCurrNode.text); END; END; END; END; END;XML file temp3.xml
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE Data SYSTEM "myDTD.dtd"> <!-- @version: --> <Data arg1="TEST"> <a Aarg1="A1 arg" Aarg2="A2 arg"> <AA AAarg1="AA1 arg">AA content</AA> <AB>AB content</AB> </a> <b>b content</b> <BA BAarg1="BA1 arg"> <BAA>BAA content</BAA> </BA> <c>c content</c> </Data>
Comments
I ran you code with xmldom 6.0 and has child return false based on your xml file. What version are you using?
What are you going to do with xml file? There are plenty examples of parsing on the web?
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n
And here are the functions.
Only XMLDocument and XMLCurrNode is global variable
XMLNodeList and I in second function are local variables.
Independent Consultant/Developer
blog: https://dynamicsuser.net/nav/b/ara3n
I also use v6.0. Yes, my example returns FALSE in the 4th indented IF...THEN. But imho it should return FALSE at the 3rd already. There is only content at the 4th level, so I expected that content would not be interpreted as additional node. Or am I wrong?
I searched the web over and over again. I think I'm going crazy because I believed that there should be tausands of examples for that. But I didn't find at least one for my case. All solutions work with XPath or something else. But I don't know the structure of the XML file, so I can't use those examples.
http://www.w3schools.com/dom/dom_nodes.asp
I will try to follow that when I retry to solve it within the next few days.