Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript+;XML:仅解析文本和混合节点(转换为JSON)_Javascript_Json_Xml Parsing - Fatal编程技术网

Javascript+;XML:仅解析文本和混合节点(转换为JSON)

Javascript+;XML:仅解析文本和混合节点(转换为JSON),javascript,json,xml-parsing,Javascript,Json,Xml Parsing,我正在使用Javascript将XML文档转换为特殊的JSON格式 使用Javascript中的标准DOM处理方法,我需要帮助构建一个高效、优雅的算法来确定节点是否包含任何非文本节点或属性,并根据节点内容以不同方式组织JSON对象 示例: <demo> <title>Text Test</title> <textonly id="demoText"> This is a text-only node! It does

我正在使用Javascript将XML文档转换为特殊的JSON格式

使用Javascript中的标准DOM处理方法,我需要帮助构建一个高效、优雅的算法来确定节点是否包含任何非文本节点或属性,并根据节点内容以不同方式组织JSON对象

示例:

<demo>
    <title>Text Test</title>
    <textonly id="demoText">
        This is a text-only node! It doesn't need a 'text' property.
    </textonly>
    <mixed id="aMixedNode">
        This is text
        <!-- This is a comment node -->
        <another />
        <!-- Note that this element has not 4, but 7 nodes -- counting the text between the non-text nodes! -->
        More text!
    </mixed>
    <textonly id="specialText" special="something">
        This would be a text-only node, but it's not because the attribute requires us to create a 'text' property
    </textonly>
    <textonly id="validText">This is a text node!</textonly>
    <nontext />
    <final>End of demo!</final>
</demo>
注意事项:

  • 所有文本值都被修剪;删除/忽略空文本值

  • 多个文本节点,即当文本和其他节点混合时,文本存储在一个数组中。如果只有一个文本值,我们就将其存储为字符串

  • ID属性用作属性名称(如果存在);否则将使用节点名称。相同节点名的多个实例(所有实例都没有ID)将存储在一个数组中(但目前未演示)

  • 与空文本节点一样,注释节点也会被删除/忽略

{
    title: 'Text Test',
    demoText: 'This is a text-only node! It doesn't need a 'text' property.',
    aMixedNode: {
        text: [
            'This is text',
            'More text!'
        ],
        another: null
    },
    specialText: {
        special: 'something',
        text: 'This would be a text-only node, but it's not because the attribute requires us to create a 'text' property'
    },
    validText: 'This is a text node!',
    nonText: null,
    final: 'End of demo!'
}