Options

Test each attribute of an XML File

cschuellercschueller Member Posts: 6
Hello !

I'm facing a problem while I'm reading an XML file with the XMLDOM automation. My file contains lot of attributes for one tag, for example :
<order client-id="1" order-external="xxxyyy123" order-id="1188333" customer-id="212221" customer-group="9999" short-name="HUFR05" order-date="24.05.2011" order-type="1" order-release-code="0" reference1="BA 11-1398" reference2="85 LA SAGESSE MS" >
  <position client-id="1" customer-id="212221" order-id="1188333" order-position="10" item-id="0162993-0" item- group="710" description1="Universal-Geschirrstapler USTH/ 57-28" description2="Edelstahl" ordered-quantity="1" desired-delivery-date="28.06.2011" delivery-date="28.06.2011" open-quantity="1" delivered-quantity="0" invoiced-quantity="0" quantity-unit-code="1" quantity-unit="Stck" gross-price="2142" value="963,9" unit-price="963,9" price-unit-code="0" discount1="55" discount2="0" vat="0" user="JBR" modification-date="24.05.2011"/>
</order>

I'd like to find a way to browse the attributes, because some are always filled but some are not always in the file, and if I try to get an attribute which doesn't exist, I get a nice error...

My code is :
CREATE(XMLDomDoc);
  XMLDomDoc.load(RecPEDIVendorSetup."WINEDI File Folder" + '\' + RecLFile.Name);
  XMLNodeList := XMLDomDoc.selectNodes('envelope/request/response/orders/order');
  XMLNodeCurr := XMLNodeList.nextNode();

  WHILE NOT ISCLEAR(XMLNodeCurr) DO
  BEGIN
    XMLAttributes := XMLNodeCurr.attributes;
    IF RecLPurchaseHeader.GET(RecLPurchaseHeader."Document Type"::Order, XMLAttributes.getNamedItem('order-external').text) THEN
    BEGIN
      IF EVALUATE(DatLDelivery, XMLAttributes.getNamedItem('delivery-date').text) THEN;
       
      //*** Some more code ***//

  end;  
  XMLNodeCurr := XMLNodeList.nextNode();
  END;

Here if the "delivery-date" attribute is not in the "order" tag then NAV throw me an error.

Thanks for your help !
Cédric

Answers

  • Options
    MBergerMBerger Member Posts: 413
    i had the same problem not too long ago, and made a function that will give you the value of an attribute if available, otherwise it'll return an empty text
        PROCEDURE GetAttribFromNode(Node : Automation :'Microsoft XML, v6.0.IXMLDOMNode ; AttribName : Text[50]) : Text[1024];
        VAR
          Element : Automation : Microsoft XML, v6.0.IXMLDOMElement;
          Attrib  : Automation : Microsoft XML, v6.0.IXMLDOMAttribute;
        BEGIN
          //You can only get the attribute if you treat a node as an element
          Element := Node ;
    
          Attrib := Element.getAttributeNode(AttribName) ;
    
          //variable is not set if attribute doesn't exist
          IF NOT(ISCLEAR(Attrib)) THEN
            EXIT(Attrib.nodeValue) ;
        END;
    
  • Options
    cschuellercschueller Member Posts: 6
    Thanks a lot, but in the meantime I found myself a solution ! Which is :

    XMLAttributes := XMLNodeCurr.attributes;
    i := 0
    WHILE i < XMLAttributes.length DO BEGIN
          case XMLAttributes.item(i).nodeName of
            'order-external': CodLOrderno := XMLAttributes.item(i).text;
            'delivery-date': IF not EVALUATE(DatLDelivery, XMLAttributes.item(i).text) THEN
                datldelivery := 0D;
            'order-id' : CodLOrderId := XMLAttributes.item(i).text;
          end;
          i += 1;
        END;
    
Sign In or Register to comment.