我正在使用MOXY JAXB使用DynamicEntity基于XSD架构生成XML文件。在编组时,我正在接受NPE。仅当我设置使用“СвУчСдТип”类型的字段时才会发生NPE。如果未设置此类型的字段,则编组成功。
这是我用来重现此错误的最小化XSD。
<?xml version="1.0" encoding="windows-1251"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sch="http://purl.oclc.org/dsdl/schematron" xmlns:usch="http://www.unisoftware.ru/schematron-extensions" xmlns:sql="urn:schemas-microsoft-com:mapping-schema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="КнПокСтр">
<xs:complexType>
<xs:sequence>
<xs:element name="СвПрод" type="СвУчСдТип" minOccurs="0" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="СвУчСдТип">
<xs:choice>
<xs:element name="СведЮЛ">
<xs:complexType>
<xs:attribute name="ИННЮЛ" type="ИННЮЛТип" use="required">
</xs:attribute>
<xs:attribute name="КПП" type="КППТип" use="required">
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="СведИП">
<xs:complexType>
<xs:attribute name="ИННФЛ" type="ИННФЛТип" use="required">
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:simpleType name="ИННФЛТип">
<xs:restriction base="xs:string">
<xs:length value="12"/>
<xs:pattern value="([0-9]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ИННЮЛТип">
<xs:restriction base="xs:string">
<xs:length value="10"/>
<xs:pattern value="([0-9]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="КППТип">
<xs:restriction base="xs:string">
<xs:length value="9"/>
<xs:pattern value="([0-9]{1}[1-9]{1}|[1-9]{1}[0-9]{1})([0-9]{2})([0-9A-Z]{2})([0-9]{3})"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
NewClass.java
公共类NewClass {
public static void main(String[] args) throws JAXBException, FileNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SAXException{
PageBuilder pb = PageBuilder.createPage().свПрод("5040114973 / 504001001");
DynamicJAXBContext context = pb.getJaxbContext();
DynamicEntity build = pb.build();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.setProperty("jaxb.encoding", Charset.forName("utf-8").toString());
marshaller.marshal(build, System.out);
}
}
PageBuilder.java
public class PageBuilder {
private static DynamicJAXBContext jaxbContext;
private DynamicEntity entity;
private static Pattern datePattern = Pattern.compile("((((0[1-9]{1}|1[0-9]{1}|2[0-8]{1})\\.(0[1-9]{1}|1[0-2]{1}))|((29|30)\\.(01|0[3-9]{1}|1[0-2]{1}))|(31\\.(01|03|05|07|08|10|12)))\\.((19|20)[0-9]{2}))|(29\\.02\\.((19|20)(((0|2|4|6|8)(0|4|8))|((1|3|5|7|9)(2|6)))))");
private PageBuilder(){}
public static PageBuilder createPage() throws FileNotFoundException, JAXBException{
PageBuilder fl = new PageBuilder()
.file("C:\\Documents and Settings\\g.ioffe\\Мои документы\\АСУ ДК\\NO_NDS.8_1_003_01_05_05_02.xsd");
return fl;
}
public DynamicJAXBContext getJaxbContext() {
return jaxbContext;
}
private PageBuilder file(String uri) throws FileNotFoundException, JAXBException{
FileInputStream xsdInputStream = new FileInputStream(uri);
jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
this.entity = jaxbContext.newDynamicEntity("КнПокСтр");
return this;
}
public PageBuilder свПрод(String person){
DynamicEntity personInfo = this.parsePerson(person);
if (personInfo != null){
List persons;
if (this.entity.isSet("свПрод")){
persons = this.entity.<List>get("свПрод");
}
else persons = new ArrayList<>();
persons.add(personInfo);
this.entity.set("свПрод", persons);
}
return this;
}
public DynamicEntity build(){
return this.entity;
}
private DynamicEntity parsePerson(String person){
DynamicEntity personInfo = null;
if (person.trim().length() == 0 || person.trim().equals("/")) return null;
String[] split = person.split("/");
for(int i = 0; i < split.length; i++){
split[i] = split[i].trim();
}
if (split[0].length() == 12) {
personInfo = jaxbContext.newDynamicEntity("СведИП");
personInfo.set("иннфл", split[0]);
} else if (split[0].length() == 10){
personInfo = jaxbContext.newDynamicEntity("СведЮЛ");
String kpp;
if(split.length == 1)
kpp = "";
else kpp = split[1];
personInfo.set("иннюл",split[0]);
personInfo.set("кпп",kpp);
}
return personInfo;
}
}
栈跟踪
Exception in thread "main" javax.xml.bind.MarshalException
- with linked exception:
[java.lang.NullPointerException]
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:500)
at example.minimal.NewClass.main(NewClass.java:35)
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.dynamic.ValuesAccessor.getAttributeValueFromObject(ValuesAccessor.java:56)
at org.eclipse.persistence.mappings.DatabaseMapping.getAttributeValueFromObject(DatabaseMapping.java:657)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.getAttributeValue(ObjectMarshalContext.java:51)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:142)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:443)
at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:243)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:334)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:104)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:149)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:102)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:59)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:443)
at org.eclipse.persistence.internal.oxm.XPathObjectBuilder.buildRow(XPathObjectBuilder.java:243)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:118)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:1)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:766)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshalStreamOrWriter(XMLMarshaller.java:1147)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:934)
at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:877)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:496)
... 1 more