我正在尋找一個 XML 決議器,它將允許以扁平化的方式處理單個標簽決議。東西這個模塊會,但服務器/ Node.js的兼容。例如:
const xmlToReact = new someXmlParser({
Example: (attrs) => {console.log("get all necessary info")},
Item: (attrs) => {console.log("get all necessary info")}
});
const reactTree = someXmlParser.convert(`
<Example name="simple">
<Item i="1">one</Item>
<Item>two</Item>
<Item>three</Item>
</Example>
`);
更新:
謝謝團英陳!在Camaro的模塊也幾乎所有我所期待的。我可以問你進一步的問題嗎?
我的 XML 如下所示:
<para style="p">
<verse number="1" style="v" />В начале Всевышний
<note caller=" " style="f">
<char style="fr" closed="false">1:1 </char>
<char style="fk" closed="false">Всевышний </char>
<char style="ft" closed="false">– на языке оригинала: ?Элохим? – слово, родственное арабскому ?Аллах?. См. приложение V.</char>
</note> сотворил небо и землю.
<verse number="2" style="v" />Земля была безлика и пуста, тьма была над бездной, и Дух Всевышнего парил над водами.
</para>
<para style="p">
<verse number="3" style="v" />И сказал Всевышний: ?Да будет свет?, и появился свет.
<verse number="4" style="v" />Всевышний увидел, что свет хорош, и отделил его от тьмы.
<verse number="5" style="v" />Он назвал свет ?днём?, а тьму – ?ночью?. И был вечер, и было утро – день первый.
</para>
<para style="p">
<verse number="6" style="v" />И сказал Всевышний: ?Да будет свод между водами, чтобы отделить воду от воды?.
<verse number="7" style="v" />Всевышний создал свод и отделил воду под сводом от воды над ним; и стало так.
<verse number="8" style="v" />И Он назвал свод ?небом?. И был вечер, и было утро – день второй.
</para>
注意verse標簽。它是一個自閉標簽。我正在尋找的結果應如下所示:
{
"verses": [
{
"number": "1",
"text": "text that runs till the next occurrence of the verse tag"
},
{
"number": "2",
"text": "text that runs till the next occurrence of the verse tag"
},
{
"number": "3",
"text": "text that runs till the next occurrence of the verse tag"
}
]
}
我怎樣才能用camara決議器完成它?
uj5u.com熱心網友回復:
我認為camaro會滿足您的需求。您可以通過 xpath 模板重塑 xml。
例如
const { transform } = require('camaro')
const xml = `
<Example name="simple">
<Item i="1">one</Item>
<Item>two</Item>
<Item>three</Item>
</Example>
`
const template = {
examples: ['/Example', {
name: '@name',
items: ['Item', '.']
}]
}
async function main() {
const output = await transform(xml, template)
console.log(JSON.stringify(output, null, 2))
}
main()
{
"examples": [
{
"name": "simple",
"items": [
"one",
"two",
"three"
]
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/316693.html
下一篇:更改嵌套節點的值
