【问题标题】:DTD. Where did i make a mistake?DTD。我在哪里做错了?
【发布时间】:2015-03-21 16:19:23
【问题描述】:

我尝试使用 DOM 解析器解析这个 xml 文件,我的代码返回了正确的结果,但是在运行时,ErrorHandler 调用“error”方法打印我“error”,这意味着我在某个地方犯了错误。我认为是我在 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);


}

}

【问题讨论】:

  • System.out.println(exception); 将提供有关错误的更多信息。

标签: java xml dom dtd


【解决方案1】:

您的 XML 对其 DTD 内部子集无效。

进行以下更改以使其生效:

  1. display 之前添加fontwindow 元素,或更改 configuration 的内容模型不需要这些元素。 (一世 后者在下面做了。)
  2. IDs 使用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>

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2022-01-17
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多