Proper use of XMLDOM

Miklos_HollenderMiklos_Hollender Member Posts: 1,598
Basically I'm just using XMLDOM by a trial-by-error method because I have no idea on the Document Object Model theory behind it.

Therefore the following code works, but can somebody "beautify it" a bit, i.e. tell me how I would use it if I would understand what I'm doing?
<PurchaseOrderLineMessage>
  <PurchaseOrderNo>PO0000001</PurchaseOrderNo>
  <PurchaseOrderLineNo>20000</PurchaseOrderLineNo>
   et cetera, et cetera...
</PurchaseOrderLineMessage>
XMLDomDoc.loadXML(XMLMessage);
FOR i := 0 TO XMLDomDoc.childNodes.item(0).childNodes.length-1 DO BEGIN
    CLEAR(XMLNode);
    XMLNode := XMLDomDoc.childNodes.item(0).childNodes.item(i);
    CASE XMLNode.nodeName OF
         'PurchaseOrderNo' : PurchOrderNo:=LTMNode.text;
         'PurchaseOrderLineNo' : EVALUATE(PurchOrderLineNo,XMLNode.text);
  et cetera, et cetera...
    END;
END;

Because if I keep doing it this way, then in a deeper structure I'll have to write XMLDomDoc.childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(0).childNodes.item(i);
and who wants to do that :)

Comments

  • GoMaDGoMaD Member Posts: 313
    You might take a look at the following codeunits which are standard:

    6224 XML DOM Management
    6225 XML Document Encode
    6226 XML Document Decode

    They give a good picture on how you should use the XML DOM object.

    Regards,
    Now, let's see what we can see.
    ...
    Everybody on-line.
    ...
    Looking good!
  • DenSterDenSter Member Posts: 8,307
    Use a node object, and browse into the tree with that object, so you're always one step away from your target node. Take a look at the XML codeunits for examples.
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Thanks!
  • DenSterDenSter Member Posts: 8,307
    Did you find what you needed? I'm a bit busy at the moment, but could get you a short code sample later if you need it.
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Well, it seems they are heavily using the selectSingleNode method. I'm not completely sure it's right. As far as I understand, the whole point about XML is the hierarchy, and selectSingleNode looks like a simple "free text search".

    Let's assume we have a <table> <field> <field/> <field> </field></table>hierarchy. We write code to read that. Then later on turns out that we need in fact multiple tables and wrap a <tables> </tables> node around them. Then the selectSingleNode approach needs to be completely rewritten, while my rude and ugly approach needs only another FOR loop wrapped around it.
  • ara3nara3n Member Posts: 9,258
    you could use xml ports. it has shortcommings, but I believe for people to learn either DOM or xmlports, it would be easier to learn xmlports.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • DenSterDenSter Member Posts: 8,307
    Well Miklos it seems that if it is not done your way then it is by definition flawed :mrgreen:

    SelectSingleNode selects a single node into a node object, including all subnodes of that node. It is not a free text search, it is an MSDOM object that can hold more than 1024 characters for instance, because it's not a text variable.

    In the XML codeunits you'll find examples of loops, once you know how it's done it's not that hard.

    Ahmed, I disagree with you about the use of XMLPorts vs. MSDOM. Using the MSDOM is more code, but does not have the limitations of XMLPorts. You just have more control. The trick is to know when to use which one, and that's not easy to do :).
Sign In or Register to comment.