欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nodejs實(shí)現(xiàn)解析xml字符串為對(duì)象的方法示例

 更新時(shí)間:2018年03月14日 14:43:12   作者:dongmelon  
這篇文章主要介紹了nodejs實(shí)現(xiàn)解析xml字符串為對(duì)象的方法,涉及nodejs針對(duì)xml格式字符串的解析與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了nodejs實(shí)現(xiàn)解析xml字符串為對(duì)象的方法。分享給大家供大家參考,具體如下:

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() );
});

沒(méi)啥新奇的,看看輸出吧

第一句輸出結(jié)果為:

{
  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

根據(jù)輸出我們就可以猜這東西是怎么回事兒了。

1、xmlreader將xml轉(zhuǎn)換為JSON對(duì)象(這樣表述不準(zhǔn)確,但是大家知道怎么一回事兒)。
2、轉(zhuǎn)換成的JSON對(duì)象的嵌套結(jié)構(gòu)與原xml標(biāo)簽嵌套結(jié)構(gòu)相同。
3、視xml中同一級(jí)別出現(xiàn)某標(biāo)簽次數(shù)不同(一次和多次)生出不同的對(duì)應(yīng)對(duì)象,如上的node為一次,who為三次。
4、提供了一下函數(shù)供操作屬性或者遍歷等等。

各方法含義:

1、attributes:獲取所有屬性。
2、parent:獲取父節(jié)點(diǎn)。
3、count:獲取數(shù)目。
4、at:獲取下標(biāo)為指定值的節(jié)點(diǎn)。
5、each:遍歷,參數(shù)為一個(gè)函數(shù)。
6、text:獲取節(jié)點(diǎn)內(nèi)的文本,僅當(dāng)前節(jié)點(diǎn)的文本,不包含子節(jié)點(diǎn)的文本。

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • node.js中的fs.lstatSync方法使用說(shuō)明

    node.js中的fs.lstatSync方法使用說(shuō)明

    這篇文章主要介紹了node.js中的fs.lstatSync方法使用說(shuō)明,本文介紹了fs.lstatSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • 使用nodejs?spider爬取圖片及數(shù)據(jù)實(shí)現(xiàn)

    使用nodejs?spider爬取圖片及數(shù)據(jù)實(shí)現(xiàn)

    這篇文章主要為大家介紹了使用nodejs?spider爬取圖片及數(shù)據(jù)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • npm出現(xiàn)Cannot?find?module?'XXX\node_modules\npm\bin\npm-cli.js'錯(cuò)誤的解決方法

    npm出現(xiàn)Cannot?find?module?'XXX\node_modules\npm\bin\np

    最近在啟動(dòng)項(xiàng)目的時(shí)候會(huì)報(bào)這個(gè)錯(cuò)就是npm丟失,所以下面這篇文章主要給大家介紹了關(guān)于npm出現(xiàn)Cannot?find?module?'XXX\node_modules\npm\bin\npm-cli.js'錯(cuò)誤的解決方法,需要的朋友可以參考下
    2022-08-08
  • 詳解Node.js包的工程目錄與NPM包管理器的使用

    詳解Node.js包的工程目錄與NPM包管理器的使用

    這篇文章主要介紹了Node.js包的工程目錄與NPM包管理器的使用,可以幫助編程者更好地組織Node.js代碼文件,需要的朋友可以參考下
    2016-02-02
  • Node.js視頻流應(yīng)用創(chuàng)建之后端的全過(guò)程

    Node.js視頻流應(yīng)用創(chuàng)建之后端的全過(guò)程

    這篇文章主要給大家介紹了關(guān)于創(chuàng)建Node.js視頻流應(yīng)用之后端的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 基于Node.js搭建hexo博客過(guò)程詳解

    基于Node.js搭建hexo博客過(guò)程詳解

    這篇文章主要介紹了基于Node.js搭建hexo博客過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,
    2019-06-06
  • node中socket.io的事件使用詳解

    node中socket.io的事件使用詳解

    這篇文章主要介紹了node中socket.io的事件使用詳解,需要的朋友可以參考下
    2014-12-12
  • Node.js?源碼閱讀深入理解cjs模塊系統(tǒng)

    Node.js?源碼閱讀深入理解cjs模塊系統(tǒng)

    這篇文章主要為大家介紹了Node.js?源碼閱讀深入理解cjs模塊系統(tǒng),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • node使用Koa2搭建web項(xiàng)目的方法

    node使用Koa2搭建web項(xiàng)目的方法

    本篇文章主要介紹了node使用Koa2搭建web項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • node.js中的fs.statSync方法使用說(shuō)明

    node.js中的fs.statSync方法使用說(shuō)明

    這篇文章主要介紹了node.js中的fs.statSync方法使用說(shuō)明,本文介紹了fs.statSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12

最新評(píng)論