Email:longsu2010 at yeah dot net
xml作为一种重要的数据交换格式,我就不相信没人用node.js解析过它。我就随便搜索了一下,果然很多,什么把xml转成json啊,等等吧。我看了下,xmlreader这个模块比较简单,功能上满足我的需求。
使用方法简介如下:
1、安装。npm install xmlreader
2、使用。看代码
var xmlreader = require("xmlreader");
var fs = require("fs");
var xml_string = '<response id="1" shop="aldi">'
+ 'This is some other content'
+ '<who name="james">James May</who>'
+ '<who name="sam">'
+ 'Sam Decrock'
+ '<location>Belgium</location>'
+ '</who>'
+ '<who name="jack">Jack Johnsen</who>'
+ '<games age="6">'
+ '<game>Some great game</game>'
+ '<game>Some other great game</game>'
+ '</games>'
+ '<note>These are some notes</note>'
+ '</response>';
xmlreader.read(xml_string, function(errors, response){
if(null !== errors ){
console.log(errors)
return;
}
console.log( response.response );
console.log( response.response.text() );
});
没啥新奇的,看看输出吧
第一句输出结果为:
{
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]
}
}
第二句输出:
This is some other content
根据输出我们就可以猜这东西是怎么回事儿了。
1、xmlreader将xml转换为JSON对象(这样表述不准确,但是大家知道怎么一回事儿)。
2、转换成的JSON对象的嵌套结构与原xml标签嵌套结构相同。
3、视xml中同一级别出现某标签次数不同(一次和多次)生出不同的对应对象,如上的node为一次,who为三次。
4、提供了一下函数供操作属性或者遍历等等。
各方法含义:
1、attributes:获取所有属性。
2、parent:获取父节点。
3、count:获取数目。
4、at:获取下标为指定值的节点。
5、each:遍历,参数为一个函数。
6、text:获取节点内的文本,仅当前节点的文本,不包含子节点的文本。
分享到:
相关推荐
Video8.Demo1.XmlReader
\r\n 3.1.1 .NET Framework 2.0对XML支持的设计目的\r\n 3.1.2 XML命名空间 \r\n 3.1.3 XML解析 \r\n 3.1.4 编写XML\r\n 3.1.5 XPath支持\r\n 3.1.6 XML模式对象模型\r\n 3.1.7 理解XML验证 \r\n 3.1.8 用...
Extjs解析xml问题 Ext.onReady(function(){ var store=new Ext.data.Store({ url:'GridData.xml', reader:new Ext.data.XmlReader( {record:'Item'}, ["ASIN","Author","Manufacturer","Product...
XML文件阅读器,用于查看xml文件内表格内容,可拖拽文件。
在JAVA中有两种常见的XML解析方式,DOM和SAX,DOM在解析的时候会将所有的数据一次性载入内存中进行解析,在数据量比较大的情况下,效率非常低.尤其在手机这种对内存和性能要求比较苛刻的设备里面这种方法并不可取. ...
NULL 博文链接:https://suky.iteye.com/blog/621169
public class XmlReader { /* * fileName文件的路徑和名稱 * firstChild為xml第一個節點 * */ public Element ReaderXml(String fileName,String firstChild){ Element element=null; try{ File xmlFile = ...
Silverlight使用XmlReader读取XML并加载到TreeView
php xmlReader & xmlWriter
NULL 博文链接:https://ladybird2010.iteye.com/blog/757745
用C#操作xml xmlFilePath = textBox2.Text FileStream fs = new FileStream(filePath ,FileMode.... XmlTextWriter xmlreader = new XmlTextWriter(xmlFilePath+"IMAGEENCODING.xml" ,System .Text .Encoding .UTF8 )
使用 XML Reader 读取 XML 文档,有多个XML Reader 读取 XML 文档的例子,适用于初学者。
System.Xml.XmlReader myReader; ... myReader = mySqlCommand.ExecuteXmlReader(); DataSet DataSet myDataSet = new DataSet(); 读 myDataSet.ReadXml("C:\myData.XML"); 结构 myDataSet.ReadXmlSchema("C:\...
XMLReader,XML解析,用于soap请求返回XML数据的解析
SimpleXMLReader, 包装的XMLReader类,用于对大型xml进行简单的SAX读取 简单的XML阅读器包装器XMLReader类,用于简单的英镑 SAX -reading ( 简单的XPath -queries ),代价为大。对其他xml库的内存使用的最小 ( 。...
google天气预报XML-Pull解析版(完全解析)
j2me开发时解晰xml时用到的jar包
using (XmlReader reader = new XmlNodeReader(this._xmlDocumentdoc.DocumentElement)) { dsxml = new DataSet(); dsxml.ReadXml(reader); reader.Close(); } if (dsxml != null) return dsxml.Tables[0];...
XmlReader读取Xml需要通过Read()实例方法,不断读取Xml文档中的声明,节点开始,节点内容,节点结束,以及空白等等,直到文档结束,Read()方法返回false。如下读取Xml内容实例代码和注释说明 代码如下:using System;...
Video8.Demo1.XmlReader