dtd文件中的XML解析出错“与元素类型相关联的属性”{1}“需要打开引用...”

时间:2014-05-14 07:27:25

标签: xml sax saxparseexception

我的address.dtd文件如下。当我尝试使用SAXParserFactory解析我的address.xml文件时,异常变为,

“org.xml.sax.SAXParseException; systemId:file:/home/samitha/svnrepo/XML/XMLParserTest/src/address.dtd; lineNumber:9; columnNumber:22;对于属性”{1,需要打开引号}“与元素类型”省“相关联。”

<!-- address.dtd -->
<!ELEMENT address (name,house-id?,village,city,postal-code,country) >
<!ELEMENT name (personal:title*,(first-name|first-init),last-name+)>
<!ELEMENT house-id (houses:title,NAME)>
<!ELEMENT personal:name (#PCDATA) >
<!ELEMENT village (#PCDATA) >
<!ELEMENT city (#PCDATA) >
<!ATTLIST city district CDATA #REQUIRED 
               province CDATA (Southern|Western|Nothern|Eastern) "One Sri 
Lanka"> 
<!ELEMENT postal-code (#PCDATA) >
<!ELEMENT country (#PCDATA) >
<!ELEMENT personal:title (#PCDATA) >
<!ELEMENT first-name (#PCDATA) >
<!ELEMENT first-init (#PCDATA) >
<!ELEMENT last-name (#PCDATA) >
<!ELEMENT houses:title (#PCDATA) >
<!ELEMENT NAME (#PCDATA) >

address.xml文件如下所示。

<?xml version ="1.0" encoding="UTF-8"?>
<!DOCTYPE address SYSTEM "address.dtd">


<!-- Some namespaces are declared in this-->
<address 
    xmlns:personal="Personal things"
    xmlns:houses="Regarding to houses"

>
    <name  xml:space='preserve'>  
        <personal:title>Mr.     </personal:title>
        <first-name>Samitha       </first-name>
        <last-name>Chathuranga</last-name>
    </name>
    <house-id>
        <houses:title>107 B</houses:title>
        <NAME>Sam&apos;s Home</NAME>
        <!--  An intnal entity is used for the single quote in House Name here-->
    </house-id>
    <village>Poramba</village>
    <city district="Galle" province="Southern">AG</city>
    <postal-code>80300</postal-code>
    <country>Sri Lanka</country>
</address>

1 个答案:

答案 0 :(得分:2)

属性的数据类型可以是枚举或CDATA,但不能同时存在。您应该删除CDATA

<!ATTLIST city 
      district CDATA #REQUIRED 
      province (Southern|Western|Nothern|Eastern) "One Sri Lanka"> 

来源: http://www.xmlfiles.com/dtd/dtd_attributes.asp

然而这里仍然存在问题。 “One Sri Lanka”的值与您在此列出的四个值中的任何一个都不匹配。你背后的想法是什么?