如果你写入文件 .xml 你没有产生错误,但如果你有一个 .dtd 文件你必须清除元素!正确的文件 test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (book)+>
<!ELEMENT book (title, authors, year_published, ISBN, number_of_pages, price) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (author)>
<!ELEMENT author (first_name, last_name, gender)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT year_pusblished (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT number_of_pages (#PCDATA)>
<!ELEMENT price (discount)>
<!ATTLIST price discount (yes|no) "no">]>
<catalog>
<book>
<title></title>
<authors>
<author>
<first_name></first_name>
<last_name></last_name>
<gender></gender>
</author>
</authors>
<year_published></year_published>
<ISBN></ISBN>
<number_of_pages></number_of_pages>
<price>
<discount></discount>
</price>
</book>
</catalog>
正确的文件 test.dtd (链接到 xml 文件,声明到相同的 test.xml ):
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT catalog (book)+>
<!ELEMENT book (title, authors, year_published, ISBN, number_of_pages, price) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (author)>
<!ELEMENT author (first_name, last_name, gender)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT year_pusblished (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT number_of_pages (#PCDATA)>
<!ELEMENT price (discount)>
<!ATTLIST price discount (yes|no) "no">