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.