XML DOM: hasChildNodes returns always TRUE
                
                    einsTeIn.NET                
                
                    Member Posts: 1,050                
            
                        
            
                    I've got a problem with XML DOM and I found this post where the same problem is described. It looks like he found a solution but I can't get it.
My problem is that I want to parse a XML file and I don't know its exact structure. Every element could have childs and I don't know how many. I must create some kind of recursive function to step along the tree of the XML file. Maybe someone created a solution for that already :?:
I thought I could use
I created a test function and a test XML file...
                My problem is that I want to parse a XML file and I don't know its exact structure. Every element could have childs and I don't know how many. I must create some kind of recursive function to step along the tree of the XML file. Maybe someone created a solution for that already :?:
I thought I could use
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?
I created a test function and a test XML file...
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>
                "Money is likewise the greatest chance and the greatest scourge of mankind."
0                
            Comments
- 
            Hello Einstein
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?0 - 
            Here is an example on how to parse it.
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); GetNodes(XMLCUrrNode); END;
And here are the functions.GetChildNodes(XMLCurrNode : Automation "'Microsoft XML, v6.0'.IXMLDOMNode") if XMLCurrNode.hasChildNodes then begin GetNodes(XMLCurrNode.firstChild); end else MESSAGE('Node: %1\Text: %2',XMLCurrNode.nodeName,XMLCurrNode.text); GetNodes(XMLCurrNode : Automation "'Microsoft XML, v6.0'.IXMLDOMNode") XMLNodeList := XMLCurrNode.childNodes; For I := 0 to XMLNodeList.length do begin GetChildNodes(XMLCurrNode); XMLCurrNode := XMLNodeList.nextNode; end;
Only XMLDocument and XMLCurrNode is global variable
XMLNodeList and I in second function are local variables.0 - 
            Thanks for your reply.
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.
                        "Money is likewise the greatest chance and the greatest scourge of mankind."0 - 
            
 - 
            Oh! Now it's clear. I just misunderstood the meaning of node. Thank you for your reply. Very good link.
I will try to follow that when I retry to solve it within the next few days."Money is likewise the greatest chance and the greatest scourge of mankind."0 
Categories
- All Categories
 - 73 General
 - 73 Announcements
 - 66.6K Microsoft Dynamics NAV
 - 18.7K NAV Three Tier
 - 38.4K NAV/Navision Classic Client
 - 3.6K Navision Attain
 - 2.4K Navision Financials
 - 116 Navision DOS
 - 851 Navision e-Commerce
 - 1K NAV Tips & Tricks
 - 772 NAV Dutch speaking only
 - 617 NAV Courses, Exams & Certification
 - 2K Microsoft Dynamics-Other
 - 1.5K Dynamics AX
 - 323 Dynamics CRM
 - 111 Dynamics GP
 - 10 Dynamics SL
 - 1.5K Other
 - 990 SQL General
 - 383 SQL Performance
 - 34 SQL Tips & Tricks
 - 35 Design Patterns (General & Best Practices)
 - 1 Architectural Patterns
 - 10 Design Patterns
 - 5 Implementation Patterns
 - 53 3rd Party Products, Services & Events
 - 1.6K General
 - 1.1K General Chat
 - 1.6K Website
 - 83 Testing
 - 1.2K Download section
 - 23 How Tos section
 - 252 Feedback
 - 12 NAV TechDays 2013 Sessions
 - 13 NAV TechDays 2012 Sessions
 
