Home  >  Article  >  Web Front-end  >  Example of how nodejs implements parsing xml strings into objects

Example of how nodejs implements parsing xml strings into objects

亚连
亚连Original
2018-05-30 10:56:371606browse

This article mainly introduces the method of nodejs to parse xml strings into objects, involving the operation skills related to the parsing and conversion of xml format strings by nodejs. Friends in need can refer to the following

Examples of this article Use nodejs to implement the method of parsing xml string into object. Share it with everyone for your reference, the details are as follows:

var xmlreader = require("xmlreader");
var fs = require("fs");
var xml_string = &#39;<response id="1" shop="aldi">&#39;
      +    &#39;This is some other content&#39;
      +    &#39;<who name="james">James May</who>&#39;
      +    &#39;<who name="sam">&#39;
      +      &#39;Sam Decrock&#39;
      +      &#39;<location>Belgium</location>&#39;
      +    &#39;</who>&#39;
      +    &#39;<who name="jack">Jack Johnsen</who>&#39;
      +    &#39;<games age="6">&#39;
      +      &#39;<game>Some great game</game>&#39;
      +      &#39;<game>Some other great game</game>&#39;
      +    &#39;</games>&#39;
      +    &#39;<note>These are some notes</note>&#39;
      +  &#39;</response>&#39;;
xmlreader.read(xml_string, function(errors, response){
  if(null !== errors ){
    console.log(errors)
    return;
  }
  console.log( response.response );
  console.log( response.response.text() );
});

Nothing new, just take a look at the output

The output result of the first sentence is :

{
  attributes : [Function],
  parent : [Function],
  count : [Function],
  at : [Function],
  each : [Function],
  text : [Function],
  who : {
    array : [[Object], [Object], [Object]],
    count : [Function],
    at : [Function],
    each : [Function]
  },
  games : {
    attributes : [Function],
    parent : [Function],
    count : [Function],
    at : [Function],
    each : [Function],
    game : {
      array : [Object],
      count : [Function],
      at : [Function],
      each : [Function]
    }
  },
  note : {
    attributes : [Function],
    parent : [Function],
    count : [Function],
    at : [Function],
    each : [Function],
    text : [Function]
  }
}

The second sentence output:

This is some other content

We can guess based on the output What's going on with this thing?

1. xmlreaderConvert xml to JSON object (this expression is not accurate, but everyone knows what it is).
2. The nested structure of the converted JSON object is the same as the nested structure of the original xml tag.
3. Depending on the number of times a certain tag appears at the same level in xml (once and multiple times), different corresponding objects will be generated. The above node is once and who is three times.
4. Provide some functions for operating attributes or traversing, etc.

Method meaning:

1, attributes: Get all attributes.
2, parent: Get the parent node.
3, count: Get the number.
4, at: Get the node whose subscript is the specified value.
5, each: Traversal, the parameter is a function.
6, text: Get the text within the node, only the text of the current node, excluding the text of the child nodes.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Angular uses the operation event instruction ng-click to pass multiple parameters example

JavaScript code to implement txt File upload preview function

Angularjs implementation example summary of communication between controllers

The above is the detailed content of Example of how nodejs implements parsing xml strings into objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn