簡體   English   中英

驗證xml時出錯:根元素前面的文檔中的標記必須格式正確

[英]Error while validating xml: The markup in the document preceding the root element must be well-formed

因此,我正在創建一個Java程序,該程序讀取並驗證XML文件,然后對其執行操作。 我已經解析了XML文件,但是當我嘗試驗證時,我仍然遇到相同的錯誤。

根元素之前的文檔中的標記必須格式正確。

當我在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;
    }

}

模式是指XSD,即XML格式的模式。 錯誤注釋關於找不到正確的XML。

我想默認情況下,默認的builder.isValidating()將為true。 添加一個ErrorHandler。 省略驗證碼,然后在infox中重命名info即可查看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM