Example of how nodejs implements parsing xml strings into objects

亚连
Release: 2018-05-30 10:56:37
Original
1545 people have browsed it

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 = '' + '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() ); });
Copy after login

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] } }
Copy after login

The second sentence output:

This is some other content
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!