<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pets[
<!ELEMENT pets (dogs,penguins)>
<!ELEMENT dogs(dog*)>
<!ELEMENT penguins(penguin+)>
<!ELEMENT dog(name,health,love,strain?)>
<!ATTLIST dog id CDATA #REQUIRED>
<!ELEMENT penguin(name,health,love,sex)>
<!ATTLIST penguin id CDATA #REQUIRED>
<!ELEMENT name(#PCDATA)>
<!ELEMENT health(#PCDATA)>
<!ELEMENT love(#PCDATA)>
<!ELEMENT strain(#PCDATA)>
<!ELEMENT sex(#PCDATA)>
]>
<pets>
<dogs>
<dog id="1">
<name>YAYA</name>
<health>100</health>
<love>0</love>
<strain>酷酷的雪娜瑞</strain>
</dog>
<dog id="2">
<name>OUOU</name>
<health>90</health>
<love>15</love>
<strain>聪明的拉布拉多犬</strain>
</dog>
</dogs>
<penguins>
<penguin id="3">
<name>QQ</name>
<health>100</health>
<love>20</love>
<sex>Q仔</sex>
</penguin>
</penguins>
</pets>
这是我的xml文件的开头部分,当我将这段自定义的文档类型说明删除后再解析的话就完全正确,如果不删除就报错 ,错误如下:
[Fatal Error] pet2.xml:4:15: White space is required after the element type "dogs" in the element type declaration.
org.xml.sax.SAXParseException: White space is required after the element type "dogs" in the element type declaration.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
at TestMain.main(TestMain.java:43)
请问如果我想保留这段<!Doctype>的话,我该如何修改?
------解决方案--------------------
这段说明能否不删而是注释掉?
或者也给他定义一个元素框把它包裹起来
------解决方案--------------------
这个xml好熟悉,两年前写过。。这里一看就是 dtd定义的规范与下面的标签有冲突导致的。
刚刚试了下发现标签名称和括号必须空格分开,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pets[
<!ELEMENT pets (dogs,penguins)>
<!ELEMENT dogs (dog*)>
<!ELEMENT penguins (penguin+)>
<!ELEMENT dog (name,health,love,strain?)>
<!ATTLIST dog id CDATA #REQUIRED>
<!ELEMENT penguin (name,health,love,sex)>
<!ATTLIST penguin id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT health (#PCDATA)>
<!ELEMENT love (#PCDATA)>
<!ELEMENT strain (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
]>
<pets>
<dogs>
<dog id="1">
<name>YAYA</name>
<health>100</health>
<love>0</love>
<strain>酷酷的雪娜瑞</strain>
</dog>
<dog id="2">
<name>OUOU</name>
<health>90</health>
<love>15</love>
<strain>聪明的拉布拉多犬</strain>
</dog>
</dogs>
<penguins>
<penguin id="3">
<name>QQ</name>
<health>100</health>
<love>20</love>
<sex>Q仔</sex>
</penguin>
</penguins>
</pets>
效果如下:
------解决方案--------------------