ConstructingParser:无法从File加载有效的XML文档

时间:2016-12-02 22:53:08

标签: xml scala

我的XML看起来像

 $cat /tmp/person.xml
 <root xmlns="http://www.nyorg.com/metadata/config" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd">
    <!-- This is important comment about person-->
    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>john.doe@noone.com</email>
      <city>SomeWhere</city>
      <property name="myName" value="someValue" />
    </person>
  </root>

我尝试将其加载为

val source = io.Source.fromURL(new java.io.File("/tmp/person.xml").toURL)
val d = scala.xml.parsing.ConstructingParser.fromSource(source, preserveWS = true)
val doc = d.document()

我看到的是

:1:1: < expected<root xmlns="http://www.nyorg.com/metadata/config" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd">^
doc: scala.xml.Document = null

另一方面,当我尝试使用XML.loadFile加载它时,它可以正常工作

val doc = XML.loadFile("/tmp/person.xml")
println(doc)  

我看到了

<root xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns="http://www.nyorg.com/metadata/config">

    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>john.doe@noone.com</email>
      <city>SomeWhere</city>
      <property value="someValue" name="myName"/>
    </person>
  </root>

ConstructingParser我做错了什么?

1 个答案:

答案 0 :(得分:0)

产生所有差异的线是

<?xml version="1.0" encoding="UTF-8"?>

我一旦将文件更改为包含此行

cat /tmp/person.xml
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.nyorg.com/metadata/config" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd">
    <!-- This is important comment about person-->
    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>john.doe@noone.com</email>
      <city>SomeWhere</city>
      <property name="myName" value="someValue" />
    </person>
  </root>

我的代码开始工作

root: scala.xml.Node = <root xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns="http://www.nyorg.com/metadata/config">
    <!-- This is important comment about person-->
    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>john.doe@noone.com</email>
      <city>SomeWhere</city>
      <property value="someValue" name="myName"/>
    </person>
  </root>

问题仍然是即使ConstructingParser重新排序属性,但那是另一个StackOverflow问题