我尝试用DOM解析器解析这个xml文件,我的代码返回正确的结果但是在运行时,ErrorHandler调用“错误”方法打印我“错误”这意味着我在某处犯了一个错误。我认为我在DTD中的错误,但我不知道在哪里。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration[
<!ELEMENT configuration (font, window, display)+>
<!ELEMENT display (font, window)>
<!ATTLIST display id ID #REQUIRED>
<!ELEMENT font (name, size)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ELEMENT window (height, width)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT width (#PCDATA)>
]>
<configuration>
<display id="112">
<font>
<name>Arial</name>
<size>48</size>
</font>
<window>
<height>1080</height>
<width>1920</width>
</window>
</display>
<display id="2893">
<font>
<name>ArialBlack</name>
<size>25</size>
</font>
<window>
<height>480</height>
<width>640</width>
</window>
</display>
</configuration>
public class DisplayConfig {
private static String path;
private String fontName;
private int fontSize;
private int height;
private int width;
public static void setPath(String path) {
DisplayConfig.path = path;
}
public DisplayConfig(){
}
public DisplayConfig(String fontName, int fontSize, int width, int height) {
this.fontName = fontName;
this.fontSize = fontSize;
this.height = height;
this.width = width;
}
@Override
public String toString() {
return "[Resolution:" + this.width + "x" + this.height + ", Font:" + this.fontName + ", Font size:" + this.fontSize + "]";
}
public static DisplayConfig getDisplayConfig(int id) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
System.out.println("warning");
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println("fatal error");
}
@Override
public void error(SAXParseException exception) throws SAXException {
System.out.println("error");
}
});
Document document = builder.parse(path);
Element root = document.getDocumentElement();
NodeList displays = root.getElementsByTagName("display");
Element display = null;
for(int i = 0; i<displays.getLength(); i++){
Element d = (Element) displays.item(i);
if(Integer.parseInt(d.getAttribute("id")) == id){
display = d;
break;
}
}
Element font = (Element) display.getElementsByTagName("font").item(0);
String fontName = font.getElementsByTagName("name").item(0).getTextContent();
int fontSize = Integer.parseInt(font.getElementsByTagName("size").item(0).getTextContent());
Element window = (Element) display.getElementsByTagName("window").item(0);
int width = Integer.parseInt(window.getElementsByTagName("width").item(0).getTextContent());
int height = Integer.parseInt(window.getElementsByTagName("height").item(0).getTextContent());
return new DisplayConfig(fontName, fontSize, width, height);
}
}
答案 0 :(得分:0)
您的XML根据其DTD内部子集无效。
进行以下更改以使其有效:
font
之前添加window
和display
个元素,或者更改。{
configuration
的内容模型不需要这些元素。 (一世
下面做了后者。)ID
使用NCNames(以字母开头)或禁用名称空间。 (一世
在下面做了前者。)<强>共强>:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration[
<!ELEMENT configuration (display)+>
<!ELEMENT display (font, window)>
<!ATTLIST display id ID #REQUIRED>
<!ELEMENT font (name, size)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ELEMENT window (height, width)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT width (#PCDATA)>
]>
<configuration>
<display id="d112">
<font>
<name>Arial</name>
<size>48</size>
</font>
<window>
<height>1080</height>
<width>1920</width>
</window>
</display>
<display id="d2893">
<font>
<name>ArialBlack</name>
<size>25</size>
</font>
<window>
<height>480</height>
<width>640</width>
</window>
</display>
</configuration>