使用架构问题进行XSD验证

时间:2015-02-18 12:58:39

标签: java xml jaxb unmarshalling xsd-validation

我有一个像

这样的xml文件
<root>
  <requestId>1</requestId>
  <subRequest>
    <id>11</id>
    <date>18-02-2015</date>
  </subRequest>
  <subRequest>
    <id>12</id>
    <date>19-02-2015</date>
  </subRequest>
  .
  .
</root>

我有一个我无法更改的XSD文件,在该文件中日期必须采用某种格式。并且可以有1000个“subRequest”标记条目。我创建了一个模式验证来检查格式。

所以我的问题出在这1000个条目中,如果只有2个条目的日期格式不正确,我怎么知道这2个条目的id。

当我使用JAXB(unmarshaller)将此xml转换为bean时,我正在检查这个。我使用了模式验证,validator.getLocalizedMessage()为对象和节点都给出了null。我只能看到lineNumber和关于问题格式的一般信息。

3 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

<强>的Unmarshaller.Listener

您可以创建Unmarshaller.Listener来跟踪正在解组的当前SubRequest

import javax.xml.bind.Unmarshaller;

public class SubRequestListener extends Unmarshaller.Listener {

    private SubRequest subRequest;

    @Override
    public void beforeUnmarshal(Object target, Object parent) {
        if(target.getClass() == SubRequest.class) {
            subRequest = (SubRequest) target;
        } else {
            subRequest = null;
        }
    }

    public SubRequest getSubRequest() {
        return subRequest;
    }

}

<强> XmlAdapter

然后实现XmlAdapter以验证Date属性的内容。在此示例中,我假设您将其存储为String。如果您将其存储为其他内容(例如Date),请将XmlAdapter<String, String>更改为XmlAdapter<Date, String>。此XmlAdapter引用Unmarshaller.Listener以获取SubRequest的实例。

import java.text.SimpleDateFormat;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateValidator extends XmlAdapter<String, String> {

    private SubRequestListener subRequestListener;

    public DateValidator() {
        this.subRequestListener = new SubRequestListener();
    }

    public DateValidator(SubRequestListener subRequestListener) {
        this.subRequestListener = subRequestListener;
    }

    @Override
    public String marshal(String value) throws Exception {
        return value;
    }

    @Override
    public String unmarshal(String value) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        try {
            sdf.applyPattern(value);
        } catch(IllegalArgumentException e) {
            SubRequest subRequest = subRequestListener.getSubRequest();
            System.out.println(subRequest.getId());
        }
        return value;
    }

}

<强>子请求

以下是您引用XmlAdapter

的方法
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
public class SubRequest {

    private int id;

    @XmlJavaTypeAdapter(DateValidator.class)
    private String date;

    public int getId() {
        return id;
    }

}

<强>演示

下面是一些关于如何设置的演示代码。

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();

        SubRequestListener unmarshallerListener = new SubRequestListener();
        unmarshaller.setListener(unmarshallerListener);

        DateValidator dateAdapter = new DateValidator(unmarshallerListener);
        unmarshaller.setAdapter(dateAdapter);

        File xml = new File("input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root,  System.out);
    }

}

答案 1 :(得分:1)

我知道您正在寻找基于JAXB的解决方案。 但是,如果您只想查找特定错误,还可以使用简单的SAXParser快速跟踪它。也许它可以帮助您找到导致错误的原因。

修改

我在DefaultHandler添加了一个字段,允许您获取引发错误的子请求ID。 正如我所说,这应该有助于追踪错误,它不会赢得美容奖。

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;

public class RequestParser {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

        Schema schema = null;

        try {
            String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory factory = SchemaFactory.newInstance(language);
            schema = factory.newSchema(new File("YOUR_SCHEMA.xsd"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setSchema(schema);

        SAXParser parser = spf.newSAXParser();

        parser.parse(new File("YOUR_DOCUMENT.xml"), new DefaultHandler(){

            String currentNodeQName = "";
            String currentSubrequestId = "";

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                currentNodeQName = qName;
            }

            public void characters(char[] ch, int start, int length) throws SAXException {
                if (currentNodeQName.equals("id")) {
                    currentSubrequestId = new String(ch, start, length);
                }
            }

            public void warning(SAXParseException e) throws SAXException {
                System.out.println("Warning in subrequest with id " + currentSubrequestId);
                e.printStackTrace();
            }

            public void error(SAXParseException e) throws SAXException {
                System.out.println("Error in subrequest with id: " + currentSubrequestId);
                e.printStackTrace();
            }

            public void fatalError(SAXParseException e) throws SAXException {
                System.out.println("Fatal error in subrequest with id " + currentSubrequestId);
                e.printStackTrace();
            }
        });
    }
}

您还可以在这三个特殊区块设置断点,这将为您提供有关手头错误的更多信息。

编辑2

示例输出:

Error in subrequest with id 12
org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '19-02-2015' is not a valid value for 'date'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:423)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3188)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3103)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3013)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2156)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:824)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1789)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2950)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:513)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:815)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:744)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:128)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:543)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:331)
    at so28836220.RequestParser.main(RequestParser.java:43)

答案 2 :(得分:1)

您可以执行以下操作:

Unmarshaller.Listener&amp;的ValidationEventHandler

您可以为此用例创建组合Unmarshaller.ListenerValidationEventHandler

import javax.xml.bind.*;

public class SubRequestValidator extends Unmarshaller.Listener implements ValidationEventHandler {

    private SubRequest subRequest;

    @Override
    public void beforeUnmarshal(Object target, Object parent) {
        if(target.getClass() == SubRequest.class) {
            subRequest = (SubRequest) target;
        } else {
            subRequest = null;
        }
    }

    public SubRequest getSubRequest() {
        return subRequest;
    }

    @Override
    public boolean handleEvent(ValidationEvent validationEvent) {
        if(subRequest != null) {
            System.out.println(subRequest.getId());
            System.out.println(validationEvent.getMessage());
        }
        return validationEvent.getSeverity() != ValidationEvent.FATAL_ERROR;
    }

}

然后,除了Unmarshaller的实例外,您还需要在Schema上设置两者。

    SubRequestValidator subRequestValidator = new SubRequestValidator();
    unmarshaller.setListener(subRequestValidator);
    unmarshaller.setEventHandler(subRequestValidator);

演示代码

以下是完成示例的一些支持材料:

XML架构(schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
    <element name="root">
        <complexType>
            <sequence>
                <element name="requestId" type="int"/>
                <element name="subRequest" maxOccurs="unbounded">
                    <complexType>
                        <sequence>
                            <element name="id" type="int"/>
                            <element name="date" type="date"/>
                        </sequence>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>
</schema>

XML(input.xml)

<root>
  <requestId>1</requestId>
  <subRequest>
    <id>11</id>
    <date>2015-02-18</date>
  </subRequest>
  <subRequest>
    <id>12</id>
    <date>WRONG</date>
  </subRequest>
  <subRequest>
    <id>13</id>
    <date>2015-02-19</date>
  </subRequest>
</root>

<强>根

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    private int requestId;

    @XmlElement(name="subRequest")
    private List<SubRequest> subRequests;

}

<强>子请求

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class SubRequest {

    private int id;
    private String date;

    public int getId() {
        return id;
    }

}

<强>演示

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.validation.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("src/forum28584265/schema.xsd")); 
        unmarshaller.setSchema(schema);

        SubRequestValidator subRequestValidator = new SubRequestValidator();
        unmarshaller.setListener(subRequestValidator);
        unmarshaller.setEventHandler(subRequestValidator);

        File xml = new File("src/forum28584265/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);
    }

}

<强>输出

12
cvc-datatype-valid.1.2.1: 'WRONG' is not a valid value for 'date'.
Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'WRONG' is not a valid value for 'date'.]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
    at forum28584265.Demo.main(Demo.java:23)
Caused by: org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'WRONG' is not a valid value for 'date'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:423)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3188)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3103)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:3013)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2156)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:824)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:566)
    at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:101)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:156)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:604)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1789)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2950)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:513)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:815)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:744)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:128)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:543)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
    ... 6 more