【问题标题】:while validation, i need element name in "exception"验证时,我需要“异常”中的元素名称
【发布时间】:2014-03-20 11:12:44
【问题描述】:

这是java类文件。

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.io.*;

public class Jaxp_1
{
public static void main(String [] args) throws Exception
{
Source schemaFile = new StreamSource(new File("xsd/img.xsd"));
Source xmlFile = new StreamSource(new File("xml/imgone.xml"));

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();

try{
        validator.validate(xmlFile);
        System.out.println(xmlFile.getSystemId()+ " is valid");
        System.out.println();
    }
    catch (SAXException e) 
    {
        System.out.println(schemaFile.getSystemId() + " is NOT valid");
        System.out.println("Reason: " + e.getMessage());
    }
 }
 }

这是 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns="http://www.example.org/img">
<image x="143.05" y="2" height="66" width="537"
    xhref="/dccp_repository/dam/other/images/insurance.jpg" id="Image_48"
    isLocked="false" rx="143.05" ry="2" rotation="0" />
</edge>

这是 XSD 文件。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/img"
xmlns:tns="http://www.example.org/img" elementFormDefault="qualified">
<element name="edge">
    <complexType>
        <sequence>
            <element name="image">
                <complexType>
                    <attribute name="x" type="int"></attribute>
                    <attribute name="y" type="int"></attribute>
                    <attribute name="height" type="int"></attribute>
                    <attribute name="width" type="int"></attribute>
                    <attribute name="xhref" type="string"></attribute>
                    <attribute name="id" type="string"></attribute>
                    <attribute name="isLocked" type="string"></attribute>
                    <attribute name="rx" type="double"></attribute>
                    <attribute name="ry" type="int"></attribute>
                    <attribute name="rotation" type="int"></attribute>
                </complexType>
            </element>
        </sequence>
    </complexType>
</element>
</schema>

我更改了属性类型,例如属性名称="x" type="int"。我得到了这样的错误:

file:/D:/Maheshkumar.V/workspace/JavaOne/JavaIO/xsd/img.xsd 无效 原因:cvc-datatype-valid.1.2.1:'143.05' 不是 'integer' 的有效值

它不会在异常中显示元素名称。整数在哪里?它不是特定的“图像”元素。

如果我有很大的 xml。我如何识别错误?

【问题讨论】:

    标签: java xml xsd xsd-validation


    【解决方案1】:

    抛出的异常是 SAXParseException,它是 SAXException 的子类。 SAXException 无法告诉您故障发生的位置,但 SAXParseException 可以通过 getLineNumber() 和 getColumnNumber() 告诉您。这些不会命名故障元素,但允许您通过其在 xml 文件中的位置来识别它。行号和列号指向元素的结束标记。

    你可以用这个:

    try{
        validator.validate(xmlFile);
        System.out.println(xmlFile.getSystemId()+ " is valid");
        System.out.println();
    } catch (SAXParseException e) 
    {
        System.out.println(schemaFile.getSystemId() + " is NOT valid");
        System.out.println("Reason: " + e.getMessage() 
             + " at line:" + e.getLineNumber() 
             + " at column:" + e.getColumnNumber() +".");
    } catch (SAXException e) 
    {
        System.out.println(schemaFile.getSystemId() + " is NOT valid");
        System.out.println("Reason: " + e.getMessage());
    }
    

    【讨论】:

    • >它对我有用。这可以获取元素名称吗?
    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多