DTD. Where did i make a mistake?



I try to parse this xml file with DOM parser and My code return me correct result but in runtime, ErrorHandler call "error" method which print me "error" it mean that I I somewhere made a mistake. I think that my mistake in DTD, but I don't know where.





<?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);


}


}


No comments:

Post a Comment