xml – 使用JAXB进行部分解组
发布时间:2020-12-16 07:44:14 所属栏目:百科 来源:网络整理
导读:我想部分解组大 XML. XML具有以下结构: Records Contract ... /Contract Contract ... /Contract ... Contract ... /Contract Contract ... /Contract/Records 使用XJC生成结果类: - Records |- Contract 如果我按照these(来自jaxb-ri的样本),我得到错误:
我想部分解组大
XML.
XML具有以下结构: <Records> <Contract> ... </Contract> <Contract> ... </Contract> ... <Contract> ... </Contract> <Contract> ... </Contract> </Records> 使用XJC生成结果类: - Records |- Contract 如果我按照these(来自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> 如果我使用: <jaxb:globalBindings localScoping="toplevel"/> 我收到错误: 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. 但我需要改变很多课程.这不是解决方案. /** * 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(); } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |