Node. js parses xml (xmlreader)

Source: Internet
Author: User
Tags dot net
Email: longsu2010atyeahdotnetxml, as an important data exchange format, I do not believe that no one has parsed it using node. js. I just searched for it. There are a lot of things. What should I do to convert xml into json. I can see that the xmlreader module is relatively simple,... SyntaxHighlighte

Email: longsu2010 at yeah dot net


Xml is an important data exchange format. I don't believe that no one has parsed it using node. js. I just searched for it. There are a lot of things. What should I do to convert xml into json. I have read that the xmlreader module is simple and functional to meet my needs.

The usage is described as follows:

1. installation. Npm install xmlreader

2. Use. View code


Var xmlreader = require ("xmlreader ");
Var fs = require ("fs ");

Var xml_string =' '
+ 'This is some other Content'
+' James May '
+' '
+ 'Sam Decrock'
+' Belgium '
+' '
+' Jack Johnsen '
+' '
+' Some great game '
+' Some other great game '
+' '
+' These are some notes '
+' ';


Xmlreader. read (xml_string, function (errors, response ){
If (null! = Errors ){
Console. log (errors)
Return;
}
Console. log (response. response );
Console. log (response. response. text ());
});


Nothing novel. Let's 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]
}
} Second sentence output:

This is some other content

 

 

Based on the output, we can guess what happened.

1. xmlreader converts xml into a JSON object (this statement is not accurate, but everyone knows what to do ).

2. The nested structure of the converted JSON object is the same as that of the original xml tag.

3. Different corresponding objects are generated according to the number of tags at the same level in xml (one time and multiple times). The preceding node is one time, and the who is three times.

4. provides functions for Operation attributes or traversal.

 


Meaning of each method:

1. attributes: Get all attributes.

2. parent: Get the parent node.

3. count: Obtain the number.

4. at: Get the node with the specified subscript.

5. each: traversal. The parameter is a function.

6. text: Get the text in the node. Only the text of the current node does not contain the text of the subnode.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.