这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Server SYSTEM "Server.dtd">
<Server>
<MaximumUserNumber>2</MaximumUserNumber>
<ServerPortNumber>1234</ServerPortNumber>
<MessagesQueueSize>5</MessagesQueueSize>
</Server>
这是我的 Server.dtd:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT Server
(MaximumUserNumber,
ServerPortNumber,
MessagesQueueSize)>
<!ELEMENT MaximumUserNumber (#PCDATA)>
<!ELEMENT ServerPortNumber (#PCDATA)>
<!ELEMENT MessagesQueueSize (#PCDATA)>
这是我的代码,它给了我一个 NullPointerException:
public Server() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
Document document = dbf.newDocumentBuilder().parse("config.xml");
document.normalizeDocument();
NodeList nl = document.getElementsByTagName("MaximumUserNumber");
nl.item(0); // this line causes exception
}
}
我做错了什么?谢谢!