2012-06-05 118 views
0

我想使用JavaScript解析浏览器内置解析器的XML字符串。我的XML字符串看起来是这样的: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中没有错误。

+0

您使用的浏览器是?你如何加载XML?什么是你得到确切的错误信息? – 2012-06-05 02:28:34

+0

为什么不使用[W3C验证服务](http://validator.w3.org/)?使用由编写该标准的组织支持的服务是有意义的。 – RobG

+0

我使用的是Chrome 19. parseFromString会为您加载XML。我收到以下消息:“XML分析错误:格式不正确的位置:http:// localhost:49669/XSD_Parsing.aspx行号1,列96:” –

回答

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> 
+0

我没有正确格式化XML。我忘了整件事情。你能告诉我为什么\ r \ n如此重要。谢谢。 –