Parse XML Libxmljs (Node.js)

后端 未结 1 1950
梦毁少年i
梦毁少年i 2021-02-08 10:02

I\'m attempting to parse an XML string with libxmljs (https://github.com/polotek/libxmljs). I\'m having some issues though. I need to apply logic to what I\'m parsing and return

1条回答
  •  太阳男子
    2021-02-08 10:42

    libxmljs supports DOM as well as SAX style parsing.

    var xmlDoc = libxmljs.parseXmlString('Your Name');
    var xmlDoc2 = libxmljs.parseXmlFile('mydata.xml');
    

    The API is custom and doesn't follow the W3C/browser spec (it's on my list). You will want to use xpath to query the document for the content you want.

    xmlDoc.find("//[@id='firstName']")[0].childNodes()[0].text()
    

    Notice that childNodes and text are function calls. Take a look at the docs.

    https://github.com/polotek/libxmljs/wiki/Element

    As far as I know, libxmljs and jsdom are the two libraries that have decent DOM implementations.

    0 讨论(0)
提交回复
热议问题