Xml 使用JAXB进行部分解组
我想对大XML进行部分解组 XML具有以下结构:Xml 使用JAXB进行部分解组,xml,binding,jaxb,xjc,Xml,Binding,Jaxb,Xjc,我想对大XML进行部分解组 XML具有以下结构: <Records> <Contract> ... </Contract> <Contract> ... </Contract> ... <Contract> ... </Contract> <Contract> ...
<Records>
<Contract>
...
</Contract>
<Contract>
...
</Contract>
...
<Contract>
...
</Contract>
<Contract>
...
</Contract>
</Records>
如果我遵循(来自jaxb ri的示例),我会得到错误:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://somedomain.com", local:"Contract"). Expected elements are <{http://somedomain.com}Records>
org.xml.sax.SAXParseException: A class/interface with the same name "com.my.package.Text" is already in use. Use a class customization to resolve this conflict.
但是我需要换很多课。这不是解决方案。生成顶级类 我只需要记录类和单独的合同类 默认情况下,JAXB实现将生成与匿名复杂类型对应的类作为静态内部类。如果您希望所有内容都是顶级类,则可以如您在问题中所述,使用以下外部绑定自定义:
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>
静态内部类的目的之一是防止名称冲突。对于所有顶级类,可以使用外部绑定文件重命名从复杂类型生成的类。下面是一个示例,其中与复杂类型itemType对应的类将作为Item生成
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
<jaxb:bindings schemaLocation="company.xsd">
<jaxb:bindings node="//xsd:element[@name='employee']/xsd:complexType/xsd:sequence/xsd:element[@name='address']/xsd:complexType">
<jaxb:class name="EmployeeAddress"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
了解更多信息
同名
错误。我不明白你问题的部分解组部分。要重命名的类太多了。大工程,方案可以更改->不是解决方案。@IRus-我建议保留静态内部类以减少名称冲突的数量。您好,我正在尝试类似的方法,但它并没有按预期对我起作用。我已经把问题贴在这里了。如果你有机会,请你看一看并提供你的建议?
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>
org.xml.sax.SAXParseException: A class/interface with the same name "com.my.package.Text
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
<jaxb:bindings schemaLocation="company.xsd">
<jaxb:bindings node="//xsd:element[@name='employee']/xsd:complexType/xsd:sequence/xsd:element[@name='address']/xsd:complexType">
<jaxb:class name="EmployeeAddress"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
xjc -b binding.xml your-schema.xsd
/**
* User: r.ibragimov
* Date: 04.06.13
*/
public class PartialJAXB1 {
public static void main(String[] args) throws JAXBException, XMLStreamException, FileNotFoundException {
final QName qName = new QName("http://www.domain.com","Contract");
InputStream inputStream = new FileInputStream(new File("c:\\test.xml"));
// create xml event reader for input stream
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputStream);
// initialize jaxb
JAXBContext context = JAXBContext.newInstance(Records.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XMLEvent e = null;
// loop though the xml stream
while( (e = xmlEventReader.peek()) != null ) {
// check the event is a Document start element
if(e.isStartElement() && ((StartElement)e).getName().equals(qName)) {
// unmarshall the document
Records.Contract contract = unmarshaller.unmarshal(xmlEventReader, Records.Contract.class).getValue();
System.out.println(contract);
} else {
xmlEventReader.next();
}
}
}
}