使用xmlbuilder在节点中创建嵌套XML

时间:2019-10-28 20:43:52

标签: node.js xml xml-builder

我正在使用以下框架构建xml

https://www.npmjs.com/package/xmlbuilder

当我执行以下操作

.ele('ATTACHMENTS','')
    .ele('ATTACHMENT', attachmentXML)

我还有另一个类似这个的

attachmentXML = builder.create('ATTACHMENT','','',{headless:true})
    .ele('FILECONTENT',escape(content)).up()

<ATTACHMENTS>  
  &lt;ATTACHMENT&gt;
  &lt;FILECONTENT&gt;PK%03%04%

主xml内的xml包裹在&lt内。而不是<等。如何解决此问题?

1 个答案:

答案 0 :(得分:1)

我尝试了以下代码,但似乎工作正常。您可以检查这是否是您想要的吗?

var builder = require("xmlbuilder");

var xml = builder.create("root").ele("ATTACHMENTS");

for (var i = 0; i < 10; i++) {
    xml.ele("ATTACHMENT").ele("FILECONTENT", Math.random());
}

console.log(xml.end({ pretty: true }));

输出

<?xml version="1.0"?>
<root>
  <ATTACHMENTS>
    <ATTACHMENT>
      <FILECONTENT>0.31942928777141466</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.16859524100899814</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.4068207368854062</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.044208161687968595</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.18556505055049022</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.1554230424004983</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.08717157045974977</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.27141852241595643</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.7834843228518138</FILECONTENT>
    </ATTACHMENT>
    <ATTACHMENT>
      <FILECONTENT>0.14114625574824502</FILECONTENT>
    </ATTACHMENT>
  </ATTACHMENTS>
</root>