0

I need to parse this xml by Google Script. jsonformatter.org tells me that the XML is valid

enter image description here

I want to get text of ICO but //var ico = root.getChild('Ares_odpovedi').getChild('Odpoved').getChild('VBAS').getChild('ICO').getText(); is throwing an error

The full code is

function getARES() {
var url = 'https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?'
    + 'ico=06018025'
    + '&xml=1';
var response = UrlFetchApp.fetch(url);

var responseText = response.getContentText(); //.replace(/D:/g,'');

var document = XmlService.parse(responseText);
var root = document.getRootElement();

var ico_tmp0 = root.getName();                 // value is "Ares_odpovedi"
var ico_tmp1 = root.getContentSize();          // value is 3         
var ico_tmp2 = root.getChild('Ares_odpovedi'); // value is null
var ico_tmp3 = root.getChild('Odpoved');       // value is null
//var ico = root.getChild('Ares_odpovedi').getChild('Odpoved').getChild('VBAS').getChild('ICO').getText();
//var ico = root.getChild('Odpoved').getChild('VBAS').getChild('ICO').getText();

Logger.log(response);
Logger.log(" ");
Logger.log(responseText);
}
Radek
  • 13,813
  • 52
  • 161
  • 255

1 Answers1

3

I believe your goal as follows.

  • You want to retrieve the text of ICO using Google Apps Script.

In this case, it is required to use the name space when getChild is used. When this is reflected to your script, it becomes as follows.

Modified script:

function getARES() {
  var url = 'https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?'
      + 'ico=06018025'
      + '&xml=1';
  var response = UrlFetchApp.fetch(url);

  var responseText = response.getContentText(); //.replace(/D:/g,'');

  var document = XmlService.parse(responseText);
  var root = document.getRootElement();
  
  // I modified below script.
  var ns1 = XmlService.getNamespace("/ares/xml_doc/schemas/ares/ares_answer_basic/v_1.0.3");
  var ns2 = XmlService.getNamespace("/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.3");
  var res = root.getChild("Odpoved", ns1).getChild("VBAS", ns2).getChild("ICO", ns2).getText();
  Logger.log(res)
}
  • When above script is run, 06018025 is retrieved.
  • When http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=27074358&xml=1 is used as the URL of UrlFetchApp.fetch, 27074358 is obtained.

References:

Added:

From your replying of Any idea why var res2 = root.getChild("Odpoved", ns1).getChild("VBAS", ns2).getChild("DIC", ns2).getText(); does not work?, now I noticed that your question had been changed.

In your question, you wanted to retrieve the value of ICO. But in the case for retrieving the value of DIC, it is required to check the structure of XML. Because in your script in your question, the XML from var url = 'https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?' + 'ico=06018025' + '&xml=1'; doesn't include the value of DIC. I think that this is the reason of your issue.

When you want to retrieve the value of DIC from http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=27074358&xml=1, please use the following script.

Modified script:

function getARES() {
  var url = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=27074358&xml=1';  // <--- Modified
  var response = UrlFetchApp.fetch(url);
  var responseText = response.getContentText(); //.replace(/D:/g,'');
  var document = XmlService.parse(responseText);
  var root = document.getRootElement();
  var ns1 = XmlService.getNamespace("/ares/xml_doc/schemas/ares/ares_answer_basic/v_1.0.3");
  var ns2 = XmlService.getNamespace("/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.3");
  var res = root.getChild("Odpoved", ns1).getChild("VBAS", ns2).getChild("DIC", ns2).getText();  // <--- Modified
  Logger.log(res)  // In this case, CZ27074358 is retrieved.
}

Note:

About the name space, these threads might be useful.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • hmm, so I did exactly what you except I did not use namespace. You said "in this case" namespace is required to use. Could you please explain why namespace must be use and why we have to use 2 different ones? – Radek Dec 23 '20 at 13:07
  • Any idea why `var res2 = root.getChild("Odpoved", ns1).getChild("VBAS", ns2).getChild("DIC", ns2).getText();` does not work? – Radek Dec 23 '20 at 13:30
  • @Radek Thank you for replying. I apologize for the inconvenience. I think that in your current issue for `var res2 = root.getChild("Odpoved", ns1).getChild("VBAS", ns2).getChild("DIC", ns2).getText();` might be to due to the URL for retrieving XML data. So I added one more sample script for your additional question. Could you please confirm it? If that was not the result you expect, I apologize again. – Tanaike Dec 24 '20 at 00:35
  • you answered my question with your first version of your answer. I usually accept the answer once I fully understood. Thank you so much for your patience. – Radek Dec 24 '20 at 08:49
  • What is the best way to find out in the script whether a child got a value? – Radek Dec 24 '20 at 08:53
  • @Radek Thank you for replying. I'm glad your issue was resolved. About your new question of `What is the best way to find out in the script whether a child got a value?`, unfortunately, in the current stage, I have no clear answer for this. I apologize that I cannot resolve your all questions. This is due to my poor skill. I deeply apologize for this. I would like to study more and more. When I found the best way for achieving it, I would like to tell you. – Tanaike Dec 25 '20 at 00:46
  • I found that `var dic2 = root.getChild("Odpoved", ns_are).getChild("VBAS", ns_d).getChildText("DIC", ns_d); ` will not throw an error. In case the elemenet does not have a value the variable will be null. – Radek Dec 27 '20 at 10:49