2010-12-03 39 views
15

我想validade針對W3C XML架構一個XML格式的XML文件。如何獲得錯誤的行號,而驗證對一個XML架構

下面的代碼不會發生錯誤時的工作和報告。但是我無法獲得錯誤的行號。它總是返回-1。

有沒有一種簡單的方法來得到行號?

import java.io.File; 

import javax.xml.XMLConstants; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Source; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

import org.w3c.dom.Document; 
import org.xml.sax.SAXParseException; 

    public class XMLValidation { 

     public static void main(String[] args) { 

      try { 
       DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
       Document document = parser.parse(new File("myxml.xml")); 

       SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
       Source schemaFile = new StreamSource(new File("myschema.xsd")); 

       Schema schema = factory.newSchema(schemaFile); 

       Validator validator = schema.newValidator(); 

       validator.validate(new DOMSource(document)); 

      } catch (SAXParseException e) { 
       System.out.println(e.getLineNumber()); 
       e.printStackTrace(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

的Mads,其它交涉及C#,而不是Java。 – pablosaraiva 2010-12-03 18:00:56

回答

15

我發現這個

http://www.herongyang.com/XML-Schema/Xerces2-XSD-Validation-with-XMLReader.html

,似乎提供下列資料

(包括行號)

Error: 
    Public ID: null 
    System ID: file:///D:/herong/dictionary_invalid_xsd.xml 
    Line number: 7 
    Column number: 22 
    Message: cvc-datatype-valid.1.2.1: 'yes' is not a valid 'boolean' 
    value. 
使用此代碼

/** 
* XMLReaderValidator.java 
* Copyright (c) 2002 by Dr. Herong Yang. All rights reserved. 
*/ 
import java.io.IOException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.DefaultHandler; 
import org.xml.sax.helpers.XMLReaderFactory; 
import org.xml.sax.SAXException; 
import org.xml.sax.SAXParseException; 
class XMLReaderValidator { 
    public static void main(String[] args) { 
     String parserClass = "org.apache.xerces.parsers.SAXParser"; 
     String validationFeature 
     = "http://xml.org/sax/features/validation"; 
     String schemaFeature 
     = "http://apache.org/xml/features/validation/schema"; 
     try { 
     String x = args[0]; 
     XMLReader r = XMLReaderFactory.createXMLReader(parserClass); 
     r.setFeature(validationFeature,true); 
     r.setFeature(schemaFeature,true); 
     r.setErrorHandler(new MyErrorHandler()); 
     r.parse(x); 
     } catch (SAXException e) { 
     System.out.println(e.toString()); 
     } catch (IOException e) { 
     System.out.println(e.toString()); 
     } 
    } 
    private static class MyErrorHandler extends DefaultHandler { 
     public void warning(SAXParseException e) throws SAXException { 
     System.out.println("Warning: "); 
     printInfo(e); 
     } 
     public void error(SAXParseException e) throws SAXException { 
     System.out.println("Error: "); 
     printInfo(e); 
     } 
     public void fatalError(SAXParseException e) throws SAXException { 
     System.out.println("Fattal error: "); 
     printInfo(e); 
     } 
     private void printInfo(SAXParseException e) { 
     System.out.println(" Public ID: "+e.getPublicId()); 
     System.out.println(" System ID: "+e.getSystemId()); 
     System.out.println(" Line number: "+e.getLineNumber()); 
     System.out.println(" Column number: "+e.getColumnNumber()); 
     System.out.println(" Message: "+e.getMessage()); 
     } 
    } 
} 
0

嘗試使用SAXLocator提供一個 http://download.oracle.com/javase/1.5.0/docs/api/org/xml/sax/Locator.html 解析器不是必需的,但如果他們這樣做應該報告行號

我覺得你的代碼應該包括:

// this will be called when XML-parser starts reading 
    // XML-data; here we save reference to current position in XML: 
    public void setDocumentLocator(Locator locator) { 
     this.locator = locator; 
    } 

(見http://www.java-tips.org/java-se-tips/org.xml.sax/using-xml-locator-to-indicate-current-parser-pos.html

分析器會給你一個定位,你就可以使用通用電氣公司t行號。這可能是值得打印/調試時,發生這種情況,看看是否有一個有效的定位

+0

我看到了定位器界面,但我無法聯繫定位器和驗證器。這個問題仍然沒有解決。 – pablosaraiva 2010-12-03 18:02:01

+1

你會probbaly必須設置定位器。這是一個例子。 http://www.exampledepot.com/egs/org.xml.sax/SetLocator.html我無法保證它能正常工作。如果定位器不工作,你可以將其設置在每個SAX事件,並閱讀最新的成功事件 – 2010-12-03 18:09:54

0

你可以抓到一個SaxException e。然後調用e.toString()

它會給這樣的:

org.xml.sax.SAXParseException; lineNumber:2668151; columnNumber:80;除非用於標記CDATA節的末尾,否則不得在內容中出現字符序列「]]>」。