[英]how to list all tags xml in node.js
Here is my XML: 这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- 1 = video , 2 = html , 3 = pdf -->
<stage_type>2</stage_type>
<resource_name>http://www.XXX</resource_name>
I want to get <resource_name>
. 我想获取
<resource_name>
。 I'm using xml2js . 我正在使用xml2js 。 When I use:
当我使用时:
var parser = new xml2js.Parser();
fs.readFile('./' + file, function(err, data) {
parser.parseString(data, function (err, result) {
console.dir(result);
console.log('Done');
});
});
I got <stage_type>
and no all xml file. 我得到了
<stage_type>
并且没有所有的xml文件。 ( <stage_type>
and <resource_name>
). (
<stage_type>
和<resource_name>
)。
I know that I don't have a root tag, but I need to leave it like that. 我知道我没有根标签,但是我需要这样保留它。
Your XML file isn't valid XML. 您的XML文件不是有效的XML。 It has multiple root nodes.
它具有多个根节点。 Try parsing XML like this and it should work:
尝试像这样解析XML,它应该可以工作:
<?xml version="1.0" encoding="utf-8"?>
<!-- 1 = video , 2 = html , 3 = pdf -->
<resource>
<stage_type>2</stage_type>
<resource_name>http://www.XXX</resource_name>
</resource>
You could rename that resource
node to whatever you want, but that's why xml2js
is missing the resource_name
. 您可以将该
resource
节点重命名为所需的名称,但这就是xml2js
缺少resource_name
的原因。 I don't know of any option to have xml2js
process a fragment (which might make it accept this type of XML). 我不知道让
xml2js
处理一个片段(这可能会使它接受这种类型的XML)的任何选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.