我有下一个文件:
<!ELEMENT notes (note+)>
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
和
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE notes SYSTEM "validation.dtd">
<notes>
<note>
<to>Alice</to>
<from>Bob</from>
<heading>Reminder</heading>
<body>Don't forget our meeting!</body>
</note>
<note>
<to>John</to>
<from>Jane</from>
<heading>Invalid Tag</heading>
<body>This should be </body>
</note>
<note>
<to>Tom</to>
<from>Sara</from>
<heading>Empty Tag</heading>
<body>hello</body>
</note>
</notes>
和我的 Java 验证器
import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.File;
public class SAXParserTest {
public static void main(String[] args) {
parseWithDTDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\validation.dtd");
// parseWithXSDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\index.xml",
// "C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\XSDvalidation.xsd");
}
private static void parseWithDTDValidation(String filePath) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true); // Увімкнути валідацію по DTD
SAXParser parser = factory.newSAXParser();
parser.parse(new File(filePath), new CustomHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void parseWithXSDValidation(String filePath, String schemaPath) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new File(schemaPath));
factory.setSchema(schema);
SAXParser parser = factory.newSAXParser();
parser.parse(new File(filePath), new CustomHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class CustomHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
System.out.println("Start Element: " + qName);
}
@Override
public void endElement(String uri, String localName, String qName) {
System.out.println("End Element: " + qName);
}
@Override
public void characters(char[] ch, int start, int length) {
System.out.println("Characters: " + new String(ch, start, length).trim());
}
@Override
public void error(SAXParseException e) {
System.out.println("Error: " + e.getMessage());
}
@Override
public void fatalError(SAXParseException e) {
System.out.println("Fatal Error: " + e.getMessage());
}
@Override
public void warning(SAXParseException e) {
System.out.println("Warning: " + e.getMessage());
}
}
编译器给我下一个错误:`org.xml.sax.SAXParseException;系统ID:文件:src/main/resources/validation.dtd;行号:1;列数:3;文档中根元素之前的标记必须格式正确。
我检查 .dtd 验证和 xml 文件中的每个符号吗?但找不到任何语法错误
如果有人回答我的问题我会很高兴
看起来您正在尝试解析此代码行中的
validation.dtd
文件:
parseWithDTDValidation("C:\\Users\\ilapa\\Desktop\\university\\4_course_1_sem\\Проектування Веб сервісів\\lab_5\\lab5\\src\\main\\resources\\validation.dtd");
您可能应该将路径传递给 xml 文件本身。