Java 验证时,我需要在“中输入元素名称”;例外情况;

Java 验证时,我需要在“中输入元素名称”;例外情况;,java,xml,xsd,xsd-validation,Java,Xml,Xsd,Xsd Validation,这是java类文件 这是一个xml文件 这是XSD文件 我更改了属性的类型,如attribute name=“x”type=“int”。我得到的错误如下: 文件:/D:/Maheshkumar.V/workspace/JavaOne/JavaIO/xsd/img.xsd无效 原因:cvc数据类型有效。1.2.1:“143.05”不是“整数”的有效值。 异常情况下,它不会显示元素名称。整数在哪里?它没有特定的“图像”元素 如果我有大的xml。如何识别错误 引发的异常是SAXParseExc

这是java类文件

这是一个xml文件


这是XSD文件


我更改了属性的类型,如attribute name=“x”type=“int”。我得到的错误如下:

文件:/D:/Maheshkumar.V/workspace/JavaOne/JavaIO/xsd/img.xsd无效 原因:cvc数据类型有效。1.2.1:“143.05”不是“整数”的有效值。

异常情况下,它不会显示元素名称。整数在哪里?它没有特定的“图像”元素

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


引发的异常是SAXParseException,SAXParseException的子类。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());
}

>它对我有效。这可以获取元素名吗?
<?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>
<?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>
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());
}