Proper use of XMLDOM
Miklos_Hollender
Member Posts: 1,629
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?
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
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
0
Comments
-
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!0 -
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.0
-
Thanks!0
-
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.0
-
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.0 -
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.0
-
Well Miklos it seems that if it is not done your way then it is by definition flawed

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
. 0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K 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
- 610 NAV Courses, Exams & Certification
- 1.9K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 251 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions

