IF ISCLEAR(XmlDoc) THEN CREATE(XmlDoc); XmlDoc.async(FALSE); okay := XmlDoc.load(Param.FileName); IF NOT okay THEN ERROR('That did not work'); // log an error CLEAR(EDIOrderHeader); InitNewOrder(EDIOrderHeader); XmlNodeList := XmlDoc.childNodes; FOR i := 0 TO XmlNodeList.length - 1 DO BEGIN XmlNode := XmlNodeList.item(i); ReadChildNodes(XmlNode,EDIOrderHeader); END;
OBJECT Codeunit 50195 Read XML ORDER { OBJECT-PROPERTIES { Date=10/11/17; Time=13:58:50; Modified=Yes; Version List=XML; } PROPERTIES { OnRun=BEGIN ReadXML('D:\Paso\pru2.xml'); END; } CODE { VAR i@1100000 : Integer; Acualizadas@1100001 : Integer; PROCEDURE ReadXML@1000000012(FileName@1100003 : Text[1024]); VAR XMLDoc@1100001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v3.0'.DOMDocument"; XMLDocOut@1100000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v3.0'.DOMDocument"; XMLNode@1100002 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v3.0'.IXMLDOMNode"; qText@1100004 : Text[50]; BEGIN CREATE(XMLDoc); IF NOT XMLDoc.load(FileName) THEN ERROR('ERROR loading file %1', FileName); RemoveNameSpace(XMLDoc, XMLDocOut); IF FindNode(XMLDocOut,'//ORDER_HEADER/ORDER_INFO/ORDER_ID',XMLNode) THEN BEGIN qText := XMLNode.text; MESSAGE('Order = %1', qText); END; CLEAR(XMLDoc); END; PROCEDURE RemoveNameSpace@1000000001(XMLSourceDoc@1000000001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v3.0'.DOMDocument";VAR XMLDestinationDoc@1000000000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v3.0'.DOMDocument"); VAR OutStreamStylesheet@1000000002 : OutStream; InStreamStylesheet@1000000003 : InStream; XMLStyleSheet@1000000004 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{F6D90F11-9C73-11D3-B32E-00C04F990BB4}:'Microsoft XML, v3.0'.DOMDocument"; TempTable@1000000005 : TEMPORARY Record 79; BEGIN TempTable.Imagen.CREATEOUTSTREAM(OutStreamStylesheet); TempTable.Imagen.CREATEINSTREAM(InStreamStylesheet); OutStreamStylesheet.WRITETEXT('<?xml version="1.0" encoding="UTF-8"?>'); OutStreamStylesheet.WRITETEXT('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'); OutStreamStylesheet.WRITETEXT('<xsl:output method="xml" encoding="UTF-8" />'); OutStreamStylesheet.WRITETEXT('<xsl:template match="/">'); OutStreamStylesheet.WRITETEXT('<xsl:copy>'); OutStreamStylesheet.WRITETEXT('<xsl:apply-templates />'); OutStreamStylesheet.WRITETEXT('</xsl:copy>'); OutStreamStylesheet.WRITETEXT('</xsl:template>'); OutStreamStylesheet.WRITETEXT('<xsl:template match="*">'); OutStreamStylesheet.WRITETEXT('<xsl:element name="{local-name()}">'); OutStreamStylesheet.WRITETEXT('<xsl:apply-templates select="@* | node()" />'); OutStreamStylesheet.WRITETEXT('</xsl:element>'); OutStreamStylesheet.WRITETEXT('</xsl:template>'); OutStreamStylesheet.WRITETEXT('<xsl:template match="@*">'); OutStreamStylesheet.WRITETEXT('<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>'); OutStreamStylesheet.WRITETEXT('</xsl:template>'); OutStreamStylesheet.WRITETEXT('<xsl:template match="text() | processing-instruction() | comment()">'); OutStreamStylesheet.WRITETEXT('<xsl:copy />'); OutStreamStylesheet.WRITETEXT('</xsl:template>'); OutStreamStylesheet.WRITETEXT('</xsl:stylesheet>'); IF ISCLEAR(XMLStyleSheet) THEN CREATE(XMLStyleSheet); XMLStyleSheet.load(InStreamStylesheet); IF ISCLEAR(XMLDestinationDoc) THEN CREATE(XMLDestinationDoc); XMLSourceDoc.transformNodeToObject(XMLStyleSheet,XMLDestinationDoc); END; PROCEDURE FindNode@3(XMLRootNode@1000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v3.0'.IXMLDOMNode";NodePath@1001 : Text[250];VAR FoundXMLNode@1002 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v3.0'.IXMLDOMNode") : Boolean; BEGIN IF ISCLEAR(XMLRootNode) THEN EXIT(FALSE); FoundXMLNode := XMLRootNode.selectSingleNode(NodePath); IF ISCLEAR(FoundXMLNode) THEN EXIT(FALSE) ELSE EXIT(TRUE); END; BEGIN END. } }
Answers
Could you put an example of this XML file ?
Regards
XML below, with some edits to remove customer specific names etc.
Here is the codeunit.
For some reason I cannot get your codeunit to work. It fails on the error when unable to load the file. The only change I made was to change the file/directory and the field name for the "picture" field in company information used as a temp table (which it never gets to since the error is before that).
The problem could be thatyou don't have enough permissions in the directory.
Regards
Is there a way to specify the allowable character set with the DOMDocument before the load? I'm looking at the MSDN set of methods for the objects but nothing is jumping out at me.
I changed the file from ANSI to UTF-8, inserted the u with two dots, and I loaded the file without a problem again.
If always is the same you can change to UTF-8 before loading.
Regards.