验证 xml 时出错:文档中根元素之前的标记必须格式正确
Error while validating xml: The markup in the document preceding the root element must be well-formed
所以我正在创建一个 java 程序来读取和验证 XML 文件,然后对其执行操作。我解析了 XML 文件,但是当我尝试验证时,我不断收到相同的错误。
The markup in the document preceding the root element must be
well-formed.
当我 运行 在我的代码之外对 XML 文件进行验证时,所有内容都会检查出来,但是当我 运行 在我的代码中进行验证时,我不断收到错误。
XML:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE company SYSTEM "Company.dtd">
<company>
<info>
<name>SCP Foundation</name>
<manager>O5</manager>
<location>Classified</location>
</info>
<warehouses>
<warehouse file="Warehouse13.xml" />
</warehouses>
<actions>
<action type="sort" att="decending" />
</actions>
</company>
DTD:
<!-- Our root element, company.-->
<!ELEMENT company (info, warehouses, actions)>
<!-- Ijfo which contains data about company. -->
<!ELEMENT info (name, manager, location)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT manager (#PCDATA)>
<!ELEMENT location (#PCDATA)>
<!-- Inbound are objects stated to arrive -->
<!ELEMENT warehouses ( warehouse+ )>
<!ELEMENT warehouse EMPTY >
<!ATTLIST warehouse file CDATA #REQUIRED >
<!-- Our actions, if the program is upgraded, this can easily be expanded -->
<!ELEMENT actions ( action* )>
<!ELEMENT action EMPTY>
<!ATTLIST action type CDATA #REQUIRED >
<!ATTLIST action att CDATA #REQUIRED >
代码:
public class XMLHandler {
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private boolean debug;
public XMLHandler(boolean debug)
{
this.debug = debug;
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try
{
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println("ERROR: Error while creating XML handler");
System.out.println( e.toString() );
System.exit(5);
return;
}
}
public boolean validateDTD(Document xml, String dtd)
{
if(debug)
{
System.out.println("DEBUG: Starting validation using " + dtd + " DTD file");
}
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File(dtd));
Schema schema;
if(debug)
{
System.out.println("DEBUG: Getting the DTD");
}
//TODO Figure out what goes wrong here
try {
schema = factory.newSchema(schemaFile);
} catch (SAXException e) {
System.out.println("DEBUG: Error while handling the DTD");
e.printStackTrace();
return false;
}
Validator validator = schema.newValidator();
if(debug)
{
System.out.println("DEBUG: Validating xml file");
}
try {
validator.validate(new DOMSource(xml));
System.out.println("DEBUG: XML file is valid");
} catch (SAXException e) {
System.out.println("ERROR: SAXException");
e.printStackTrace();
return false;
} catch (IOException e) {
System.out.println("ERROR: IOException");
e.printStackTrace();
return false;
}
return true;
}
public Document parseDocument(String xmlFile, String type)
{
if(debug)
{
System.out.println("DEBUG: Begining parsing operation for file " + xmlFile);
}
File xmlSource = new File(xmlFile);
if(!xmlSource.isFile())
{
System.out.println("ERROR: Given source file is not an actual file");
System.exit(6);
}
Document doc = null;
if(debug)
{
System.out.println("DEBUG: Creating document");
}
//We attempt to create the document
try
{
doc = builder.parse(xmlSource);
} catch (SAXException e)
{
System.out.println("ERROR: XML parser error");
e.printStackTrace();
doc = null;
System.exit(7);
} catch (IOException e)
{
System.out.println("ERROR: IO failure while parsing the document");
e.printStackTrace();
doc = null;
System.exit(7);
}
if(debug)
{
System.out.println("DEBUG: File parsed, moving to validation");
}
//TODO Fix validation
String dtd = "Warehouse.dtd";
if(type.equals("Company"))
{
dtd = "Company.dtd";
}
if( !validateDTD( doc, dtd) )
{
System.out.println("ERROR: XML file is not a valid" );
System.exit(8);
}
return doc;
}
}
Schema 引用XSD,XML格式的schema。关于找不到正确 XML.
的错误注释
我猜默认情况下 builder.isValidating()
是正确的。添加错误处理程序。去掉验证码,重命名infox左右的信息看看。
所以我正在创建一个 java 程序来读取和验证 XML 文件,然后对其执行操作。我解析了 XML 文件,但是当我尝试验证时,我不断收到相同的错误。
The markup in the document preceding the root element must be well-formed.
当我 运行 在我的代码之外对 XML 文件进行验证时,所有内容都会检查出来,但是当我 运行 在我的代码中进行验证时,我不断收到错误。
XML:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE company SYSTEM "Company.dtd">
<company>
<info>
<name>SCP Foundation</name>
<manager>O5</manager>
<location>Classified</location>
</info>
<warehouses>
<warehouse file="Warehouse13.xml" />
</warehouses>
<actions>
<action type="sort" att="decending" />
</actions>
</company>
DTD:
<!-- Our root element, company.-->
<!ELEMENT company (info, warehouses, actions)>
<!-- Ijfo which contains data about company. -->
<!ELEMENT info (name, manager, location)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT manager (#PCDATA)>
<!ELEMENT location (#PCDATA)>
<!-- Inbound are objects stated to arrive -->
<!ELEMENT warehouses ( warehouse+ )>
<!ELEMENT warehouse EMPTY >
<!ATTLIST warehouse file CDATA #REQUIRED >
<!-- Our actions, if the program is upgraded, this can easily be expanded -->
<!ELEMENT actions ( action* )>
<!ELEMENT action EMPTY>
<!ATTLIST action type CDATA #REQUIRED >
<!ATTLIST action att CDATA #REQUIRED >
代码:
public class XMLHandler {
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private boolean debug;
public XMLHandler(boolean debug)
{
this.debug = debug;
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try
{
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println("ERROR: Error while creating XML handler");
System.out.println( e.toString() );
System.exit(5);
return;
}
}
public boolean validateDTD(Document xml, String dtd)
{
if(debug)
{
System.out.println("DEBUG: Starting validation using " + dtd + " DTD file");
}
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File(dtd));
Schema schema;
if(debug)
{
System.out.println("DEBUG: Getting the DTD");
}
//TODO Figure out what goes wrong here
try {
schema = factory.newSchema(schemaFile);
} catch (SAXException e) {
System.out.println("DEBUG: Error while handling the DTD");
e.printStackTrace();
return false;
}
Validator validator = schema.newValidator();
if(debug)
{
System.out.println("DEBUG: Validating xml file");
}
try {
validator.validate(new DOMSource(xml));
System.out.println("DEBUG: XML file is valid");
} catch (SAXException e) {
System.out.println("ERROR: SAXException");
e.printStackTrace();
return false;
} catch (IOException e) {
System.out.println("ERROR: IOException");
e.printStackTrace();
return false;
}
return true;
}
public Document parseDocument(String xmlFile, String type)
{
if(debug)
{
System.out.println("DEBUG: Begining parsing operation for file " + xmlFile);
}
File xmlSource = new File(xmlFile);
if(!xmlSource.isFile())
{
System.out.println("ERROR: Given source file is not an actual file");
System.exit(6);
}
Document doc = null;
if(debug)
{
System.out.println("DEBUG: Creating document");
}
//We attempt to create the document
try
{
doc = builder.parse(xmlSource);
} catch (SAXException e)
{
System.out.println("ERROR: XML parser error");
e.printStackTrace();
doc = null;
System.exit(7);
} catch (IOException e)
{
System.out.println("ERROR: IO failure while parsing the document");
e.printStackTrace();
doc = null;
System.exit(7);
}
if(debug)
{
System.out.println("DEBUG: File parsed, moving to validation");
}
//TODO Fix validation
String dtd = "Warehouse.dtd";
if(type.equals("Company"))
{
dtd = "Company.dtd";
}
if( !validateDTD( doc, dtd) )
{
System.out.println("ERROR: XML file is not a valid" );
System.exit(8);
}
return doc;
}
}
Schema 引用XSD,XML格式的schema。关于找不到正确 XML.
的错误注释我猜默认情况下 builder.isValidating()
是正确的。添加错误处理程序。去掉验证码,重命名infox左右的信息看看。