错误:XML文档结构必须在同一实体中开始和结束

时间:2016-05-09 13:25:39

标签: xml

我是XML的新手并且收到以下错误:

  

错误:XML文档结构必须以相同的方式开始和结束   实体

输入XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<test>
<access1>113AL</access1>
<access2>119AL</access2>
</test>
<test>
<access2>115AL<s/access2>
<access3>116AL</access3>
</test>
<test>
<access4>118AL</access4>
<access5>119AL</access5>
</test>
<copies>
<test2>
<access>113AL</access>
<Copy>Y</Copy>
</test2>
<test2>
<access>113AX</access>
<Copy>N</Copy>
</test2>
</copies>
</root>

2 个答案:

答案 0 :(得分:3)

您的XML格式不正确。 通常,此错误表示起始标记和结束标记的范围有问题。

特别是在您的情况下,您在其中一个结束s代码中有一个迷路access2

    <access2>115AL<s/access2>

这是解决问题的XML;它现在已经很好了(并且缩进以提高可读性):

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <test>
    <access1>113AL</access1>
    <access2>119AL</access2>
  </test>
  <test>
    <access2>115AL</access2>
    <access3>116AL</access3>
  </test>
  <test>
    <access4>118AL</access4>
    <access5>119AL</access5>
  </test>
  <copies>
    <test2>
      <access>113AL</access>
      <Copy>Y</Copy>
    </test2>
    <test2>
      <access>113AX</access>
      <Copy>N</Copy>
    </test2>
  </copies>
</root>

答案 1 :(得分:0)

如果您的xml格式正确(如kjhughes答案中所述),则您始终可以运行xmllint将其美化为:

[nsaunders@rolly ~]$ 
[nsaunders@rolly ~]$ cat foo.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<test>
<access1>113AL</access1>
<access2>119AL</access2>
</test>
<test>
<access2>115AL</access2>
<access3>116AL</access3>
</test>
<test>
<access4>118AL</access4>
<access5>119AL</access5>
</test>
<copies>
<test2>
<access>113AL</access>
<Copy>Y</Copy>
</test2>
<test2>
<access>113AX</access>
<Copy>N</Copy>
</test2>
</copies>
</root>
[nsaunders@rolly ~]$ 
[nsaunders@rolly ~]$ xmllint --format foo.xml --output foo.xml
[nsaunders@rolly ~]$ 
[nsaunders@rolly ~]$ cat foo.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <test>
    <access1>113AL</access1>
    <access2>119AL</access2>
  </test>
  <test>
    <access2>115AL</access2>
    <access3>116AL</access3>
  </test>
  <test>
    <access4>118AL</access4>
    <access5>119AL</access5>
  </test>
  <copies>
    <test2>
      <access>113AL</access>
      <Copy>Y</Copy>
    </test2>
    <test2>
      <access>113AX</access>
      <Copy>N</Copy>
    </test2>
  </copies>
</root>
[nsaunders@rolly ~]$ 

仅供参考。

当然,xmllint主要用于验证文档。