如何创建没有根节点的XML字符串?

时间:2016-11-04 13:15:40

标签: javascript typescript

我正在使用TypeScript创建JavaScript函数。该函数需要返回一个包含键和值的XML字符串,这些值来自函数的参数。我希望它安全地完成,例如Terms & Conditions需要编码到Terms & ConditionsI have seen the DOMParser is recommended for processing XML

我的功能目前看起来像这样:

createDocumentXml(base64Document: string, category: string, documentName: string, documentExtension: string, userId: number, documentSizeBytes: number): string {
    let xmlTemplate =
        '<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
            '<active>true</active>' +
            '<category></category>' +
            '<content></content>' +
            '<createdByID></createdByID>' +
            '<createdDate xsi:nil="true"/>' +
            '<description></description>' +
            '<fileExtension></fileExtension>' +
            '<name></name>' +
            '<size></size>' +
        '</document>'

    // use a DOM parser to modify the XML safely (i.e. escape any reserved characters)
    let parser = new DOMParser();
    let xmlDocument = parser.parseFromString(xmlTemplate, 'text/xml');

    xmlDocument.getElementsByTagName('category')[0].textContent = category;
    xmlDocument.getElementsByTagName('content')[0].textContent = base64Document;
    xmlDocument.getElementsByTagName('createdByID')[0].textContent = userId.toString();
    xmlDocument.getElementsByTagName('description')[0].textContent = documentName;
    xmlDocument.getElementsByTagName('fileExtension')[0].textContent = documentExtension;
    xmlDocument.getElementsByTagName('name')[0].textContent = documentName;
    xmlDocument.getElementsByTagName('size')[0].textContent = documentSizeBytes.toString();

    let serializer = new XMLSerializer();
    return serializer.serializeToString(xmlDocument);
}

调用时,会返回如下字符串:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <active>true</active>
    <category>Correspondence\Emails</category>
    <content>ZmlzaCAmIGNoaXBzIQ==</content>
    <createdByID>6627774</createdByID>
    <createdDate xsi:nil="true"/>
    <description>Terms &amp; Conditions</description>
    <fileExtension>docx</fileExtension>
    <name>Terms &amp; Conditions</name>
    <size>12345</size>
</document>

如何在没有document root的情况下返回内部XML元素?

<active>true</active>
<category>Correspondence\Emails</category>
<content>ZmlzaCAmIGNoaXBzIQ==</content>
<createdByID>6627774</createdByID>
<createdDate xsi:nil="true"/>
<description>Terms &amp; Conditions</description>
<fileExtension>docx</fileExtension>
<name>Terms &amp; Conditions</name>
<size>12345</size>

我尝试省略xmlTemplate的根,但DOMParser.parseFromString需要一个。

存储此函数的结果,然后将其传递到另一个函数中,该函数通过将其插入相关位置来创建完整的XML数据(包括根节点)。

2 个答案:

答案 0 :(得分:0)

您可能会:

而不是您当前的回报
return Array
    .from(xmlDocument.children[0].children)
    .map(function(node) { return serializer.serializeToString(node); })
    .join("");

答案 1 :(得分:0)

由于(X)HTML实际上是有效的XML,因此可以在它们上使用常规DOM函数。

let serializer = new XMLSerializer();
var str = serializer.serializeToString(xmlDocument);

var div = document.createElement("div");
div.innerHTML = str;
var inner = div.getElementsByTagName('document')[0].innerHTML;
return inner;

https://jsfiddle.net/qchbuo7c/

然而,Tomolak是对的,如果可能的话,最好一次创建整个文档。