getElementsByTagName

Hello guys,

I need to select value from xmlfile.

Variables:
XMLDOC@1003 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A05-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.DOMDocument60";
DOCNode@1005 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v3.0'.IXMLDOMNode";
DOCNodeList@1006 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 3.0:{2933BF82-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v3.0'.IXMLDOMNodeList";
sessionid@1007 : Text;

Code:
DOCNodeList:=XMLDOC.getElementsByTagName('Data');
DOCNode :=DOCNodeList.item(0);
sessionid := DOCNode.selectNodes('SessionId').item(0).text;
MESSAGE(sessionid);

XML file looks like
<Data xsi:type="LoginInfoDto">
<SessionId>fQVic1y4Y7tetqTojdygAydPdZ2519s5zFryZFG5</SessionId>
</Data>

If I remove xsi:type="LoginInfoDto" from data element it's works fine, how can I do in my code that automaticly remove that line?

Answers

  • Martin_FörsterMartin_Förster Member Posts: 55
    I think this is your Solution:

    NodeName := 'SessionId'


    Function: GetNodeValueOfXMLParameters(XMLText : Text;NodeName : Text) : Text

    IF XMLText = '' THEN
    EXIT('');

    XMLDoc := XMLDoc.XmlDocument;
    XMLDoc.LoadXml(XMLText);
    XMLNode := XMLDoc.SelectSingleNode(NodeName));

    IF ISNULL(XMLNode) THEN
    ERROR(NodeNotFound);

    IF STRLEN(XMLNode.InnerText) > 0 THEN
    EXIT(XMLNode.InnerText);


    Name DataType Subtype Length
    XMLDOMMgt Codeunit XML DOM Management
    XMLDoc DotNet System.Xml.XmlDocument.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    XMLNode DotNet System.Xml.XmlNode.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    RootNode DotNet System.Xml.XmlNode.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
  • PauliusPociusPauliusPocius Member Posts: 3
    Not really understand this article, my xml file looks like this:

    <?xml version="1.0" encoding="utf-16"?>
    <AnswerObject xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    <Data xsi:type="LoginInfoDto">
    <SessionId>4a0kKV8NB0b37uwwozdFGOOPpJTAv1m8ZmSTsJBr</SessionId>
    <State>0</State>
    <StateDetails />
    <RoleId>114</RoleId>
    <ClientId>1130</ClientId>
    <DealerId>5</DealerId>
    <DealerGroupId>6</DealerGroupId>
    <MainClientId>1130</MainClientId>
    <PermissionTokens>
    <string>CANCALCULATEDISTANCE</string>
    <string>CARVIEW</string>
    <string>CATEGORYVIEW</string>
    <string>CONNECT_WEB</string>
    <string>DOCUMENT_READ</string>
    <string>DOCUMENT_SEND</string>
    <string>FAVORITEMODIFY</string>
    <string>FAVORITEVIEW</string>
    <string>FIXBUG</string>
    <string>GENERATE_REPORTS</string>
    <string>GETTELEMETRY</string>
    <string>MAP_READ</string>
    <string>MAP_WRITE</string>
    <string>MSG_READ</string>
    <string>MSG_SEND</string>
    <string>MULTI_USER</string>
    <string>REPORT_MENU_VIEW</string>
    <string>USERVIEW</string>
    <string>DRIVINGREPORTVIEW</string>
    <string>DRIVINGSETVIEW</string>
    <string>NOTWORKING_NOTIF</string>
    </PermissionTokens>
    <UserLanguage>lt</UserLanguage>
    </Data>
    <ErrorCode>OK</ErrorCode>
    </AnswerObject>

    i cannot even load Xml file with dotnet XMLdocument:=XMLdocument.XmlDocument because it's UFC-16.
    How can i read <SessionId> from this xml file?
  • RockWithNAVRockWithNAV Member Posts: 1,139
    @PauliusPocius

    There are couple of ways to this. Did you tried this way

    XmlNodeList := locautXmlDoc.getElementsByTagName('SesionIDt');
    XmlNode := locautXmlNodeList.item(0); //
    Message('%1',XmlNode.Text);

    Hopefully you can get the value.

    P.S - I got your request over my Blog too, thank you so much for that, if you still unable then please send me your Xml.
  • PauliusPociusPauliusPocius Member Posts: 3
    Thanks, this code helped me:

    XMLNodeList:= locautXmlDoc.GetElementsByTagName('SessionId');
    XMLNode:=XMLNodeList.Item(0);
    MESSAGE('%1',XMLNode.InnerText);

    But still have problem with XML file:
    <encoding="utf-16">
    when i'm trying to locautXmlDoc.load i get this error

    6y58gpykw86m.jpg

    How i can avoid this error?
  • RockWithNAVRockWithNAV Member Posts: 1,139
    Hey,

    Did you tried using UTF 8 or you are bound to use this?

    Try removing this encoding from the XML declaration. Replace

    <?xml version="1.0" encoding="utf-16"?>

    with

    <?xml version="1.0"?>
Sign In or Register to comment.