我使用webpack作为应用程序的绑定器,并使用XML文件进行配置。目前,我正在编写测试来从webpack打包的XML文件中检索配置。
引用此网页文章:
Loading data
import Data from './data.xml';
我自己测试,我用的是
要求
格式如下:
const select = require('xpath.js');
const DOMParser = require('xmldom').DOMParser
const parser = new DOMParser();
const data = require('./app.config.xml'); // <- THIS IS MY PROBLEM
const xdocument = parser.parseFromString(data.toString(), 'text/xml');
对于XML文件,“require”是如何工作的?
此代码:
console.log("DATA: " + data.toString());
生成此输出:
DATA: [object Object]
我已经看过很多关于将xml读入JSON的代码示例,但是我不知道如何处理JSON对象。我想将XML作为XMLObject进行查询,以便对其运行xpath查询。
我可以通过运行
JSON.stringify(data)
{"Application":{"$":{"name":"config-data"},"Imports":[{"Import":[{"$":{"from":"./another.config.xml"}}]}],"Con
which is not what I want. So how can I get the XML content from *data* as a string, which is what **parser.parseFromString** needs?
我的测试代码如下:
describe.only('xpath examples (zenobia)', async assert => {
const xdoc = require('./app.config.xml');
let applicationNodes = select(xdoc, "/Application"); // THIS RETURNS a DOMObject
console.log(`Found ${applicationNodes.length} Application elements`);
assert({
given: 'an inline xml document with an Application',
should: 'Select an element from the root',
condition: applicationNodes !== null
});
});
Found 0 Application elements
对于以下XML文档:
<?xml version="1.0"?>
<Application name="app">
<Imports/>
</Application>