【问题标题】:nodejs elementtree npm xml parsingnodejs elementtree npm xml解析
【发布时间】:2017-07-02 10:50:25
【问题描述】:

我正在尝试解析以下示例 XML 文件,以从中获取一些数据。以下是 XML 文件:

<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.1" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" id="SAP-HANA" resolved="1" xml:lang="en-US">
 <status date="2016-03-17">draft</status>
 <title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Guide to the Secure Configuration of SAP HANA</title>
 <version>0.1.28</version>

 <Profile id="profile1">
    <title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">text1</title>
    <select idref="This is rule 1" selected="true"/>
    <set-value idref="ssfs_master_key_timeout">20</set-value>
 </Profile> 

 <Profile id="profile2">
    <title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">text2</title>
    <select idref="this is rule1" selected="true"/>
    <select idref="this is rule1" selected="true"/>
    <select idref="this is rule1" selected="true"/>
 </Profile>
</Benchmark>

我需要从这个 XML 文件中获取所有配置文件(profile1、profile2...),然后对于每个配置文件的标题标签,我需要获取其文本内容。我正在尝试实现这样的目标:

for all profile in XML{
    get its attribute "id".
    get its <title> tag's text content.
}

例如下面是预期的输出:

profile1
text1
profile2
text2  // but in my code, it is always coming text1. I am not aware of, how to place [i] 

我可以得到身份证。但无法获取其标签的文本内容。这是我的代码:

var fs = require('fs'); 
var et = require('elementtree'); 
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;

var data, etree;

data = fs.readFileSync('my.xml').toString();
etree = et.parse(data);
var length = etree.findall('./Profile').length;
for (var i = 0; i < length; i++) {
    console.log(etree.findall('./Profile')[i].get('id'));
    console.log(etree.findtext('./Profile/title'));  // dont know, where to place [i]

//  var profile = etree.findall('./Profile')[i].get('id');
//  console.log(etree.findtext('./Profile'[@id=’profile’]'/title'));

    //console.log(etree.findall('./Profile'[i]'/title'));
    //console.log(list[i]);
}

【问题讨论】:

    标签: javascript node.js xml parsing elementtree


    【解决方案1】:

    你可以得到这样的文字:

    console.log(etree.findall('./Profile')[i].find('title').text);
    

    但我也会稍微重构一下代码,不要像这样多次调用 .findall:

    var profiles = etree.findall('./Profile');
    
    for (var i = 0; i < profiles.length; i++) {
    	var profile = profiles[i];
    
    	console.log(profile.get('id'));
    	console.log(profile.find('title').text);
    }

    希望这会有所帮助。

    【讨论】:

    • 嗨安东尼奥,我遇到了 elementTree 模块的另一个问题。您能否针对以下链接中提出的问题提出任何好的 nodejs 模块。稍后我可能需要进一步深入研究 xml 解析。我可能会再次遇到问题。或者任何好的 elemenTree 模块文档。 stackoverflow.com/questions/42553153/…
    • @HemantYadav 回答了这个问题。我会选择 xml2js。我自己没有尝试过,但它在 GitHub 上就像 2.3k(而 elementtree 只有 88 颗星)stars。
    • 嗨@Antonio Narkevich,在这个问题上的任何帮助。 stackoverflow.com/questions/45104116/…
    猜你喜欢
    • 2017-07-22
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-02-06
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多