Http Request <Solved>

PhoenixCNC
Member Posts: 12
I have a web service I can call to retrieve a XML document. In Navision 2013 which Automation variable do I have to declare.
Thanks
Thanks
0
Best Answer
-
In the file you post is missing a </tbody> between the last <tr> and </table>
With this code you can get the values you wantOnRun() GetTable; GetTable() XMLDoc := XMLDoc.XmlDocument; // Assuming your XML file is here XMLDoc.Load('c:\temp\table.xml'); // Go to the <tbody> part of the file IF FindNode(XMLDoc, '//table/tbody', XMLNode) THEN BEGIN // Get all <tr> nodes XMLNodeList := XMLNode.SelectNodes('tr'); Nodes := XMLNodeList.Count; FOR i := 0 TO Nodes-1 DO BEGIN XMLNode2 := XMLNodeList.ItemOf(i); // All <td> nodes inside the <tr> node XMLNodeList2 := XMLNode2.SelectNodes('td'); Nodes2 := XMLNodeList2.Count; FOR j := 0 TO Nodes2-1 DO BEGIN XMLNode3 := XMLNodeList2.ItemOf(j); IF j IN [0,1,2] THEN BEGIN IF NOT ISNULL(XMLNode3) THEN Values[j+1] := XMLNode3.InnerText ELSE Values[j+1] := ''; END; END; MESSAGE('%1 /%2 %3', Values[1], Values[2], Values[3]); END; END; FindNode(XMLRootNode : DotNet "System.Xml.XmlNode";NodePath : Text[250];VAR FoundXMLNode : DotNet "System.Xml.XmlNode") : Boolean FoundXMLNode := XMLRootNode.SelectSingleNode(NodePath); IF ISNULL(FoundXMLNode) THEN EXIT(FALSE) ELSE EXIT(TRUE);
And here you can get all variablesOBJECT Codeunit 50018 Read Table XHTML { OBJECT-PROPERTIES { Date=28/07/17; Time=18:18:54; Modified=Yes; Version List=WS; } PROPERTIES { OnRun=BEGIN GetTable; END; } CODE { PROCEDURE GetTable@1000000005(); VAR XMLDoc@1000000005 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"; XMLNode@1000000004 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNode2@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNode3@1000000009 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNodeList@1000000003 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList"; XMLNodeList2@1000000002 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList"; i@1000000001 : Integer; j@1000000008 : Integer; Nodes@1000000000 : Integer; Nodes2@1000000007 : Integer; Values@1000000010 : ARRAY [3] OF Text; BEGIN XMLDoc := XMLDoc.XmlDocument; // Assuming your XML file is here XMLDoc.Load('c:\temp\table.xml'); // Go to the <tbody> part of the file IF FindNode(XMLDoc, '//table/tbody', XMLNode) THEN BEGIN // Get all <tr> nodes XMLNodeList := XMLNode.SelectNodes('tr'); Nodes := XMLNodeList.Count; FOR i := 0 TO Nodes-1 DO BEGIN XMLNode2 := XMLNodeList.ItemOf(i); // All <td> nodes inside the <tr> node XMLNodeList2 := XMLNode2.SelectNodes('td'); Nodes2 := XMLNodeList2.Count; FOR j := 0 TO Nodes2-1 DO BEGIN XMLNode3 := XMLNodeList2.ItemOf(j); IF j IN [0,1,2] THEN BEGIN IF NOT ISNULL(XMLNode3) THEN Values[j+1] := XMLNode3.InnerText ELSE Values[j+1] := ''; END; END; MESSAGE('%1 /%2 %3', Values[1], Values[2], Values[3]); END; END; END; PROCEDURE FindNode@3(XMLRootNode@1000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";NodePath@1001 : Text[250];VAR FoundXMLNode@1002 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode") : Boolean; BEGIN FoundXMLNode := XMLRootNode.SelectSingleNode(NodePath); IF ISNULL(FoundXMLNode) THEN EXIT(FALSE) ELSE EXIT(TRUE); END; BEGIN END. } }
5
Answers
-
Stop using Automations USe Dotnet variables as Automations no more from 2016 version I believe.Thanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/0 -
What is the Subtype for the DotNot Variable?0
-
Here is an example:
// Request DotNet System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' // URL Text // ResponseStream DotNet System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Request := Request.HttpWebRequest; URL := 'http://boi.org.il/currency.xml'; Request := Request.Create(URL); Request.UseDefaultCredentials := TRUE; Request.Method('GET'); Request.ContentType('text/xml'); ResponseStream := Request.GetResponse().GetResponseStream();
0 -
DOTNET is obsolete too shortly.0
-
Here is an example:
// Request DotNet System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' // URL Text // ResponseStream DotNet System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Request := Request.HttpWebRequest; URL := 'http://boi.org.il/currency.xml'; Request := Request.Create(URL); Request.UseDefaultCredentials := TRUE; Request.Method('GET'); Request.ContentType('text/xml'); ResponseStream := Request.GetResponse().GetResponseStream();
Hi, thank you for this. If i wanted to take this response an edit the document and then save the file, roughly how would I do that?0 -
Looks like this URL ('http://boi.org.il/currency.xml') doesn't work, so I changed to other that works, and here is the code:OnRun() WebService; WebService() Request := Request.HttpWebRequest; URL := 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; Request := Request.Create(URL); Request.UseDefaultCredentials := TRUE; Request.Method('GET'); Request.ContentType('text/xml'); IF GetResponse(Request, XMLDoc) THEN ShowXML(XMLDoc); GetResponse(Request : DotNet "System.Net.HttpWebRequest";VAR XMLDocOut : DotNet "System.Xml.XmlDocument") : Boolean ResponseStream := Request.GetResponse().GetResponseStream(); IF NOT ISNULL(ResponseStream) THEN BEGIN CreateArrayBytes(Buffer, 2048); FileName := FileMgt.ServerTempFileName(''); FileStream := FileStream.FileStream(FileName, FileMode.Create, FileAccess.Write); readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length); WHILE readBytes > 0 DO BEGIN FileStream.Write(Buffer, 0, readBytes); readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length); END; FileStream.Close(); XMLDocOut := XMLDocOut.XmlDocument; XMLDocOut.Load(FileName); EXIT(TRUE); END; EXIT(FALSE); ShowXML(XMLDoc : DotNet "System.Xml.XmlDocument") FileName := FileMgt.ServerTempFileName(''); XMLDoc.Save(FileName); FileName2 := FileMgt.ClientTempFileName('XML'); FileMgt.DownloadToFile(FileName, FileName2); HYPERLINK(FileName2); CreateArrayBytes(VAR Buffer : DotNet "System.Array";LengthBuffer : Integer) NetType := NetType.GetType('System.Byte', FALSE); Buffer := Buffer.CreateInstance(NetType, LengthBuffer);
And here you can see the variables used.BJECT Codeunit 50005 Test HttWebRequest { OBJECT-PROPERTIES { Date=28/07/17; Time=13:02:07; Modified=Yes; Version List=WS; } PROPERTIES { OnRun=BEGIN WebService; END; } CODE { PROCEDURE WebService@1000000001(); VAR Request@1000000000 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest"; URL@1000000002 : Text[250]; XMLDoc@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"; BEGIN Request := Request.HttpWebRequest; URL := 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; Request := Request.Create(URL); Request.UseDefaultCredentials := TRUE; Request.Method('GET'); Request.ContentType('text/xml'); IF GetResponse(Request, XMLDoc) THEN ShowXML(XMLDoc); END; PROCEDURE GetResponse@1000000005(Request@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";VAR XMLDocOut@1000000002 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument") : Boolean; VAR ResponseStream@1000000008 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream"; FileStream@1000000011 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream"; FileMode@1000000010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileMode"; FileAccess@1000000009 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileAccess"; Buffer@1000000004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array"; FileName@1000000007 : Text; FileMgt@1000000005 : Codeunit 419; readBytes@1000000012 : Integer; BEGIN ResponseStream := Request.GetResponse().GetResponseStream(); IF NOT ISNULL(ResponseStream) THEN BEGIN CreateArrayBytes(Buffer, 2048); FileName := FileMgt.ServerTempFileName(''); FileStream := FileStream.FileStream(FileName, FileMode.Create, FileAccess.Write); readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length); WHILE readBytes > 0 DO BEGIN FileStream.Write(Buffer, 0, readBytes); readBytes := ResponseStream.Read(Buffer, 0, Buffer.Length); END; FileStream.Close(); XMLDocOut := XMLDocOut.XmlDocument; XMLDocOut.Load(FileName); EXIT(TRUE); END; EXIT(FALSE); END; PROCEDURE ShowXML@1000000010(XMLDoc@1000000000 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"); VAR FileName@1000000003 : Text; FileName2@1000000002 : Text; FileMgt@1000000001 : Codeunit 419; BEGIN FileName := FileMgt.ServerTempFileName(''); XMLDoc.Save(FileName); FileName2 := FileMgt.ClientTempFileName('XML'); FileMgt.DownloadToFile(FileName, FileName2); HYPERLINK(FileName2); END; LOCAL PROCEDURE CreateArrayBytes@1000000016(VAR Buffer@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array";LengthBuffer@1000000002 : Integer); VAR NetType@1000000000 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Type"; BEGIN NetType := NetType.GetType('System.Byte', FALSE); Buffer := Buffer.CreateInstance(NetType, LengthBuffer); END; BEGIN END. } }
1 -
Looks like this URL ('http://boi.org.il/currency.xml') doesn't work, so I changed to other that works, and here is the code:
Thanks for the reply, I'm getting an error saying the root element is missing, this is the top of the xml document
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<!-- saved from url=(0062)http://192.168.128.90/viewPCB.cgi?PCB=01213 BV-00009 TOP -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
is it possible to remove this before saving the file? and how?0 -
Well, I thought that the file you got was XML, but looks like is HTML.
In the procedure GetResponse, you can change the way the file is managed and instead of load a XMLDocument with it , first try to clean up a little.
If you can post the complete file I can take a look and see how to do it.
Regads.0 -
I have code to edit the document to make it xml friendly, however this only works when I directly load the file. How can I edit the Instream / Big text?0
-
Well, how I said in the procedure GetResponse you have a file (FileName) you can return this variable and edit with your code.0
-
Thank you for your help, I have One more question though,
My document looks like this now simplified. I've done select node as such
xmlNodeListDataHeader := xmlDoc.SelectNodes('hr/table/thead/tr/tr'); as well as xmlNodeListData := xmlDoc.SelectNodes('hr/table/tbody/tr/td');
Now what I need to do is get for each 'Location,Component, Comment'
or 'C4,PN-0000170,1NF 0402 50V C0G 5%' in each node. I understand how I can use something like txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
but how in the below example could I apply this?
<?xml version="1.0" encoding="UTF-8"?>
<hr>
<table>
<thead class="DataTable">
<tr class="DataTable">
<th class="DataTable">Line</th>
<th class="DataTable">Location</th>
<th class="DataTable">Component</th>
<th class="DataTable">Comment</th>
<th class="DataTable">Stock Location</th>
<th class="DataTable">X</th>
<th class="DataTable">Y</th>
<th class="DataTable">Z</th>
<th class="DataTable">Angle</th>
<th class="DataTable">Group</th>
<th class="DataTable">Notes</th>
</tr>
</thead>
<tbody>
<tr class="DataTable">
<th class="DataTable">1</th>
<td class="DataTable">C4</td>
<td class="DataTable">PN-0000170</td>
<td class="DataTable">1NF 0402 50V C0G 5%</td>
<td class="DataTable">0</td>
<td class="DataTableNumeric">81.318mm</td>
<td class="DataTableNumeric">47.984mm</td>
<td class="DataTableNumeric">0.000mm</td>
<td class="DataTableNumeric">-90</td>
<td class="DataTable">0</td>
</tr>
<tr class="DataTable">
<th class="DataTable">2</th>
<td class="DataTable">C9</td>
<td class="DataTable">PN-1007815</td>
<td class="DataTable">10PF 0402 50V C0G 1%</td>
<td class="DataTable">0</td>
<td class="DataTableNumeric">86.116mm</td>
<td class="DataTableNumeric">47.489mm</td>
<td class="DataTableNumeric">0.000mm</td>
<td class="DataTableNumeric">180</td>
<td class="DataTable">0</td>
</tr>
</table>
</hr>
0 -
https://forum.mibuso.com/discussion/69643/selecting-elements#latest
You already have a post for this issue.Follow me on my blog juhl.blog0 -
In the file you post is missing a </tbody> between the last <tr> and </table>
With this code you can get the values you wantOnRun() GetTable; GetTable() XMLDoc := XMLDoc.XmlDocument; // Assuming your XML file is here XMLDoc.Load('c:\temp\table.xml'); // Go to the <tbody> part of the file IF FindNode(XMLDoc, '//table/tbody', XMLNode) THEN BEGIN // Get all <tr> nodes XMLNodeList := XMLNode.SelectNodes('tr'); Nodes := XMLNodeList.Count; FOR i := 0 TO Nodes-1 DO BEGIN XMLNode2 := XMLNodeList.ItemOf(i); // All <td> nodes inside the <tr> node XMLNodeList2 := XMLNode2.SelectNodes('td'); Nodes2 := XMLNodeList2.Count; FOR j := 0 TO Nodes2-1 DO BEGIN XMLNode3 := XMLNodeList2.ItemOf(j); IF j IN [0,1,2] THEN BEGIN IF NOT ISNULL(XMLNode3) THEN Values[j+1] := XMLNode3.InnerText ELSE Values[j+1] := ''; END; END; MESSAGE('%1 /%2 %3', Values[1], Values[2], Values[3]); END; END; FindNode(XMLRootNode : DotNet "System.Xml.XmlNode";NodePath : Text[250];VAR FoundXMLNode : DotNet "System.Xml.XmlNode") : Boolean FoundXMLNode := XMLRootNode.SelectSingleNode(NodePath); IF ISNULL(FoundXMLNode) THEN EXIT(FALSE) ELSE EXIT(TRUE);
And here you can get all variablesOBJECT Codeunit 50018 Read Table XHTML { OBJECT-PROPERTIES { Date=28/07/17; Time=18:18:54; Modified=Yes; Version List=WS; } PROPERTIES { OnRun=BEGIN GetTable; END; } CODE { PROCEDURE GetTable@1000000005(); VAR XMLDoc@1000000005 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"; XMLNode@1000000004 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNode2@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNode3@1000000009 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"; XMLNodeList@1000000003 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList"; XMLNodeList2@1000000002 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList"; i@1000000001 : Integer; j@1000000008 : Integer; Nodes@1000000000 : Integer; Nodes2@1000000007 : Integer; Values@1000000010 : ARRAY [3] OF Text; BEGIN XMLDoc := XMLDoc.XmlDocument; // Assuming your XML file is here XMLDoc.Load('c:\temp\table.xml'); // Go to the <tbody> part of the file IF FindNode(XMLDoc, '//table/tbody', XMLNode) THEN BEGIN // Get all <tr> nodes XMLNodeList := XMLNode.SelectNodes('tr'); Nodes := XMLNodeList.Count; FOR i := 0 TO Nodes-1 DO BEGIN XMLNode2 := XMLNodeList.ItemOf(i); // All <td> nodes inside the <tr> node XMLNodeList2 := XMLNode2.SelectNodes('td'); Nodes2 := XMLNodeList2.Count; FOR j := 0 TO Nodes2-1 DO BEGIN XMLNode3 := XMLNodeList2.ItemOf(j); IF j IN [0,1,2] THEN BEGIN IF NOT ISNULL(XMLNode3) THEN Values[j+1] := XMLNode3.InnerText ELSE Values[j+1] := ''; END; END; MESSAGE('%1 /%2 %3', Values[1], Values[2], Values[3]); END; END; END; PROCEDURE FindNode@3(XMLRootNode@1000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";NodePath@1001 : Text[250];VAR FoundXMLNode@1002 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode") : Boolean; BEGIN FoundXMLNode := XMLRootNode.SelectSingleNode(NodePath); IF ISNULL(FoundXMLNode) THEN EXIT(FALSE) ELSE EXIT(TRUE); END; BEGIN END. } }
5 -
Thank you ftornero that is fantastic!0
-
[Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]
Regards,Alain Krikilion
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K 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
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions