0

我正在尝试使用浏览器的内置解析器使用 JavaScript 解析 XML 字符串。我的 XML 字符串如下所示:

<?xml version='1.0' encoding='UTF-8' ?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
            xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
            xsi:schemaLocation='http://www.w3.org/2001/XMLSchema XMLSchema.xsd'
            elementFormDefault='qualified'
            version='1.0'>
<xsd:element name='probeMetadata' type='OASIS.System.Processor.LinuxProcessorProbe' />
<xsd:complexType name='OASIS.System.Processor.LinuxProcessorProbe'>
<xsd:complexContent>
<xsd:extension base='OASIS.System.Processor.ProcessorProbe'>
<xsd:sequence>
    <xsd:element name='nice_time' type='xsd:unsignedLong'  />
    <xsd:element name='iowait_time' type='xsd:unsignedLong'  />
    <xsd:element name='irq_time' type='xsd:unsignedLong'  />
    <xsd:element name='soft_irq_time' type='xsd:unsignedLong'  />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name='OASIS.System.Processor.ProcessorProbe'>
<xsd:sequence>
    <xsd:element name='idle_time' type='xsd:unsignedLong'  />
    <xsd:element name='system_time' type='xsd:unsignedLong'  />
    <xsd:element name='user_time' type='xsd:unsignedLong'  />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

我编写了一个简单的 JavaScript 代码,只是为了检查解析器是否正确解析了我的 XML 并将其转换为有效的 XML DOM。JavaScript 代码如下所示:

parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");

x = xmlDoc.documentElement.childNodes;

document.getElementById("Text1").value = x[3].nodeName;

这里的“文本”在 XML 之上。这段代码没有任何意义。一开始我只是想测试一些简单的东西。我在 w3school.com 测试了 XML 的有效性,它没有给我错误,所以我想 XML 中没有错误。

4

1 回答 1

1

以下对我有用。我正在使用 Chrome 20.0.1132.21 beta-m。

<html>
<head>
    <script>
        function test(){
            var text = "<?xml version='1.0' encoding='UTF-8' ?>\r\n"
            + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'\r\n"
            + "            xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\r\n"
            + "            xsi:schemaLocation='http://www.w3.org/2001/XMLSchema XMLSchema.xsd'\r\n"
            + "            elementFormDefault='qualified'\r\n"
            + "            version='1.0'>\r\n"
            + "<xsd:element name='probeMetadata' type='OASIS.System.Processor.LinuxProcessorProbe' />\r\n"
            + "<xsd:complexType name='OASIS.System.Processor.LinuxProcessorProbe'>\r\n"
            + "<xsd:complexContent>\r\n"
            + "<xsd:extension base='OASIS.System.Processor.ProcessorProbe'>\r\n"
            + "<xsd:sequence>\r\n"
            + "    <xsd:element name='nice_time' type='xsd:unsignedLong'  />\r\n"
            + "    <xsd:element name='iowait_time' type='xsd:unsignedLong'  />\r\n"
            + "    <xsd:element name='irq_time' type='xsd:unsignedLong'  />\r\n"
            + "    <xsd:element name='soft_irq_time' type='xsd:unsignedLong'  />\r\n"
            + "</xsd:sequence>\r\n"
            + "</xsd:extension>\r\n"
            + "</xsd:complexContent>\r\n"
            + "</xsd:complexType>\r\n"
            + "<xsd:complexType name='OASIS.System.Processor.ProcessorProbe'>\r\n"
            + "<xsd:sequence>\r\n"
            + "    <xsd:element name='idle_time' type='xsd:unsignedLong'  />\r\n"
            + "    <xsd:element name='system_time' type='xsd:unsignedLong'  />\r\n"
            + "    <xsd:element name='user_time' type='xsd:unsignedLong'  />\r\n"
            + "</xsd:sequence>\r\n" + "</xsd:complexType>\r\n"
            + "</xsd:schema>"
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");
            x = xmlDoc.documentElement.childNodes;
            document.getElementById("Text1").value = x[3].nodeName;         

        }
    </script>
</head>
<body>
    <input type="button" value="click" onClick="test()"/>
    <input type="text" name="Text1" id="Text1"/>
</body>
</html>
于 2012-06-05T05:37:14.833 回答