2016-12-02 55 views
1
加載有效的XML文檔

我的XML看起來像ConstructingParser:無法從文件

$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>[email protected]</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>[email protected]</email> 
     <city>SomeWhere</city> 
     <property value="someValue" name="myName"/> 
    </person> 
    </root> 

我在做什麼毛病ConstructingParser

回答

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>[email protected]</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>[email protected]</email> 
     <city>SomeWhere</city> 
     <property value="someValue" name="myName"/> 
    </person> 
    </root> 

的問題仍然是,即使ConstructingParser重新排序的屬性,但這是另一個StackOverflow問題n