nodejs elementtree npm xml 解析合并
nodejs elementtree npm xml parsing and merging
我有一个关于我上次 post 在下面 post 编辑的相同模块的问题。看起来,处理 XML 解析是相当困难的。 elementTree 的文档非常丰富。
我有以下 template_XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Tailoring xmlns="http://checklists.nist.gov/xccdf/1.2" id="xccdf_org.open-scap_tailoring_example">
<status>incomplete</status>
<version time="2013-01-15T16:00:00.000+02:00">1.0</version>
<Profile id="PlaceHolder">
<title>Tailoring for py_test</title>
<select idref="xccdf_rule_name" selected="true"/>
</Profile>
</Tailoring>
下面是 sample_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>
我需要创建一个 new_xml 文件,一个 template_XML 文件的副本。
但是想用“profile2 替换 new_xml 文件中的“PlaceHolder”个人资料标签" sample_XML 文件的标签。它是一种合并 2 xml 文件并创建一个新文件的方法。
下面是我试过的代码:
function call(id){
var template_XML = 'C:\MyDrive\template_XML';
var new_xml = 'C:\MyDrive\new_xml';
data = fs.readFileSync(template_XML).toString();
data1 = fs.readFileSync(sample_XML).toString();
etree = et.parse(data);
etree1 = et.parse(data1);
var profile = etree.find('./Profile'); // Getting the profile sub-element.
etree.getroot().remove(profile) // Removing the sub-element. So that I can insert new profile from sample file
var profiles = etree1.findall('./Profile'); // Find the required profile.
for (var i = 0; i < profiles.length; i++) {
if(profiles[i].get('id') == 'profile2')
var tmppro = profiles[i];
}
console.log(tmppro);
etree.insert(3,tmppro); // insert it. Failing
var write = fs.openSync(new_xml, 'w');
etree.write(write); // write it. Failing
}
出于某种原因,此代码在 "etree.insert" 和 "etree.write"
方面不起作用
我终于可以让它与当前的库一起工作了。
请看我的评论:
'use strict';
const et = require('elementtree');
const path = require('path');
const fs = require('fs');
function populateXmlTemplate(id) {
//Please use path.join to make it cross-platform
var template_XML = path.join(__dirname, 'template.xml');
var sample_XML = path.join(__dirname, 'sample.xml');
var new_xml = path.join(__dirname, 'new.xml');
var data = fs.readFileSync(template_XML).toString();
var data1 = fs.readFileSync(sample_XML).toString();
var etree = et.parse(data);
var etree1 = et.parse(data1);
var root = etree.getroot();
var placeholder = root.find('./Profile');
root.remove(placeholder);
var profiles = etree1.findall('./Profile'); // Find the required profile.
for (var i = 0; i < profiles.length; i++) {
//If I get it right, it shouldn't be hardcoded
if (profiles[i].get('id') == id) {
var tmppro = profiles[i];
}
}
//After you removed the placeholder the number of children decreased
//So it should be 2, not 3.
//Also etree doesn't have insert method, please call root.insert
root.insert(2, tmppro);
//You have been writing the document a bit incorrectly.
var resultXml = etree.write();
fs.writeFileSync(new_xml, resultXml);
}
populateXmlTemplate('profile2');
module.exports = {populateXmlTemplate};
但是你是对的,文档不好。它大多不见了。所以大多数时候我只是 debugging it to see the available methods, also there are some tests 在 lib 仓库中。
还有其他模块可以使用 js。请看这个 answer.
我有一个关于我上次 post 在下面 post 编辑的相同模块的问题。看起来,处理 XML 解析是相当困难的。 elementTree 的文档非常丰富。
我有以下 template_XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Tailoring xmlns="http://checklists.nist.gov/xccdf/1.2" id="xccdf_org.open-scap_tailoring_example">
<status>incomplete</status>
<version time="2013-01-15T16:00:00.000+02:00">1.0</version>
<Profile id="PlaceHolder">
<title>Tailoring for py_test</title>
<select idref="xccdf_rule_name" selected="true"/>
</Profile>
</Tailoring>
下面是 sample_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>
我需要创建一个 new_xml 文件,一个 template_XML 文件的副本。 但是想用“profile2 替换 new_xml 文件中的“PlaceHolder”个人资料标签" sample_XML 文件的标签。它是一种合并 2 xml 文件并创建一个新文件的方法。 下面是我试过的代码:
function call(id){
var template_XML = 'C:\MyDrive\template_XML';
var new_xml = 'C:\MyDrive\new_xml';
data = fs.readFileSync(template_XML).toString();
data1 = fs.readFileSync(sample_XML).toString();
etree = et.parse(data);
etree1 = et.parse(data1);
var profile = etree.find('./Profile'); // Getting the profile sub-element.
etree.getroot().remove(profile) // Removing the sub-element. So that I can insert new profile from sample file
var profiles = etree1.findall('./Profile'); // Find the required profile.
for (var i = 0; i < profiles.length; i++) {
if(profiles[i].get('id') == 'profile2')
var tmppro = profiles[i];
}
console.log(tmppro);
etree.insert(3,tmppro); // insert it. Failing
var write = fs.openSync(new_xml, 'w');
etree.write(write); // write it. Failing
}
出于某种原因,此代码在 "etree.insert" 和 "etree.write"
方面不起作用我终于可以让它与当前的库一起工作了。 请看我的评论:
'use strict';
const et = require('elementtree');
const path = require('path');
const fs = require('fs');
function populateXmlTemplate(id) {
//Please use path.join to make it cross-platform
var template_XML = path.join(__dirname, 'template.xml');
var sample_XML = path.join(__dirname, 'sample.xml');
var new_xml = path.join(__dirname, 'new.xml');
var data = fs.readFileSync(template_XML).toString();
var data1 = fs.readFileSync(sample_XML).toString();
var etree = et.parse(data);
var etree1 = et.parse(data1);
var root = etree.getroot();
var placeholder = root.find('./Profile');
root.remove(placeholder);
var profiles = etree1.findall('./Profile'); // Find the required profile.
for (var i = 0; i < profiles.length; i++) {
//If I get it right, it shouldn't be hardcoded
if (profiles[i].get('id') == id) {
var tmppro = profiles[i];
}
}
//After you removed the placeholder the number of children decreased
//So it should be 2, not 3.
//Also etree doesn't have insert method, please call root.insert
root.insert(2, tmppro);
//You have been writing the document a bit incorrectly.
var resultXml = etree.write();
fs.writeFileSync(new_xml, resultXml);
}
populateXmlTemplate('profile2');
module.exports = {populateXmlTemplate};
但是你是对的,文档不好。它大多不见了。所以大多数时候我只是 debugging it to see the available methods, also there are some tests 在 lib 仓库中。
还有其他模块可以使用 js。请看这个 answer.