简体   繁体   中英

Parsing XML file using only built-in JavaScript functions

I'm a beginner in coding and I'm trying to parse an XML file, hosted on GitHub , using JavaScript, to extract, for instance, the information contained in the <author> node nested in the <book id="bk102"> node.

I'm aware there are several similar questions in Stackoverflow, I read many of them but what I am trying to do is to extract data from the above-mentioned XML file, using only built-in JavaScript functions.

Honestly, I don't know if it is even possible but I'll explain a bit more in details what I tried to do in order to give you the whole picture.

Why I can use only JS built-in functions?

I'm developing a dynamic survey form using a data collection platform called Fulcrum .

Fulcrum allows to write JavaScript code (with some custom functions) in its 'Data events' module in order to interact with the fields and calculations contained in the survey form itself.

The 'Data events' module in Fulcrum doesn't allow me to install additional packages. For this reason, I cannot install, for example, xml-js library, as suggested here , to use the built-in JavaScript JSON functions or I cannot use DOMParser() interface, among others, since Fulcrum wouldn't recognize it.

From where I started?

Reading Fulcrum documentation, I found out I can use acustom function called REQUEST and I referred to the following example. The Fulcrum custom functions are identified with CAPITAL LETTERS.

// This example looks up the place name from OpenStreetMap when the location changes and fills in a text
// field with the place name. Replace 'place_name' below with a text field on your form.

ON('change-geometry', function(event) {
  var options = {
    url: 'https://nominatim.openstreetmap.org/search/' + LATITUDE() + ',' + LONGITUDE(),
    qs: {
      format: 'json',
      polygon: 1,
      addressdetails: 1
    }
  };

  REQUEST(options, function(error, response, body) {
    if (error) {
      ALERT('Error with request: ' + INSPECT(error));
    } else {
      var data = JSON.parse(body);
      if (data.length) {
        SETVALUE('place_name', data[0].display_name);
      }
    }
  });
});

The only issue here is that this example reads form a JSON file, while I need to read from a XML file.

What I tried?

I tried to edit the function in the example to make it work with an XML file. This is how I modified it.

ON('click', 'import_xml', function(event) {
  var options = {
    url: 'https://gist.githubusercontent.com/Ram-N/5189462/raw/46db0b43ad7bf9cbd32a8932f3ab981bd4b4da7c/books.xml',
    qs: {
      format: 'xml'
    }
  };

  REQUEST(options, function(error, response, body) {
    if (error) {
      ALERT('Error with request: ' + INSPECT(error));
    } else {
      var data = body;
      if (data.length) {
        SETVALUE('test_field_xml', data);
      }
    }
  });
});

And it works! Partially... When I click the import_xml hyperlink (refer to 1 below), I'm able to import the whole body of the XML file in the test_field_xml field (refer to 2 below) but I dont' really know how to extract only the information contained in the <author> node nested in the <book id="bk102"> node.

在此处输入图片说明

Any advice on how to proceed would be extremely helpful. Thank you, Stefano.


EDIT1:

I think I found a very "homemade" and partial solution, also not very practical or nice but it works. I edited the code above as shown below:

ON('click', 'import_xml', function(event) {
  var options = {
    url: 'https://gist.githubusercontent.com/Ram-N/5189462/raw/46db0b43ad7bf9cbd32a8932f3ab981bd4b4da7c/books.xml',
    qs: {
      format: 'xml'
    }
  };

  REQUEST(options, function(error, response, body) {
    if (error) {
      ALERT('Error with request: ' + INSPECT(error));
    } else {
      var data = body;
      var test1 = body.substring(
            body.lastIndexOf('<book id="bk102">') + 24, 
            body.lastIndexOf('<book id="bk103">') - 15
        );
      var test2 = test1.substring(
            test1.lastIndexOf('<author>') + 8, 
            test1.lastIndexOf('</author>') - 0
        );     
      if (data.length) {
        SETVALUE('test_field_xml', test2);
      }
    }
  });
});

In your opinion, could this be done in a better and more efficient way?

You have said you are allowed to write Javascript code, but that you can't install additional Javascript packages. So the answer is to download an open-source XML parser written in Javascript and add it to your code as if it was part of the code you had written yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM