本文介绍: 解决jaxb读取xml文件报错:javax.xml.bind.UnmarshalException: 意外的元素 (uri:”urn:swift:xsd:envelope“, local:”Envelope“)。所需元素为的问题。
当使用javax.xml.bind包下的JAXBContext类对xml进行解析时候,在xml源文件的标签中存在命名空间时候,会产生javax.xml.bind.UnmarshalException: 意外的元素 (uri:”urn:swift:xsd:envelope“, local:”Envelope“)。所需元素为<{}Envelope> 问题。
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="urn:swift:xsd:envelope">
<AppHdr>
<Fr>
<BICFI>CITIUS33XXX</BICFI>
</Fr>
<To>
<BICFI>CITIUS33XXX</BICFI>
</To>
<BizMsgIdr>TESTINGSIT00003</BizMsgIdr>
<MsgDefIdr>pacs.008.001.08</MsgDefIdr>
<BizSvc>swift.cbprplus.01</BizSvc>
<CreDt>2019-01-11T12:43:41.960+00:00</CreDt>
</AppHdr>
<Document>
<PmtId>
<InstrId>1-TESTINGSIT00003</InstrId>
<EndToEndId>1-KK04044506271305</EndToEndId>
<UETR>1-174c245f-2682-4291-ad67-2a41e530cd27</UETR>
</PmtId><PmtId>
<InstrId>2-TESTINGSIT00003</InstrId>
<EndToEndId>2-KK04044506271305</EndToEndId>
<UETR>2-174c245f-2682-4291-ad67-2a41e530cd27</UETR>
</PmtId>
</Document>
</Envelope>
使用javax.xml.bind的包下的JAXBContext类的解析代码。
public class TestFileXml {
public static void main(String[] args) {
File file = new File("D:\学习记录\JDK\JavaX\测试解析XML.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(EnvelopeEntity.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
EnvelopeEntity entity = (EnvelopeEntity) unmarshaller.unmarshal(file);
System.out.println(entity.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
对象xml文件的实体类EnvelopeEntity,在实体类中我们使用javax.xml.bind.annotation包下的注释和xml文件标签进行关联。
@XmlRootElement(name= "Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
public class EnvelopeEntity {
@XmlElement(name = "AppHdr")
private AppHdrEntity appHdrEntity;
@XmlElement(name = "Document")
private DocumentEntity documentEntity;
public AppHdrEntity getAppHdrEntity() {
return appHdrEntity;
}
public void setAppHdrEntity(AppHdrEntity appHdrEntity) {
this.appHdrEntity = appHdrEntity;
}
public DocumentEntity getDocumentEntity() {
return documentEntity;
}
public void setDocumentEntity(DocumentEntity documentEntity) {
this.documentEntity = documentEntity;
}
@Override
public String toString() {
return "EnvelopeEntity{" +
"appHdrEntity=" + appHdrEntity.toString() +
", documentEntity=" + documentEntity.toString() +
'}';
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class AppHdrEntity {
@XmlElement(name = "Fr")
private FrEntity frEntity;
@XmlElement(name = "To")
private ToEntity toEntity;
@XmlElement(name = "BizMsgIdr")
private String bizMsgIdr;
@XmlElement(name = "MsgDefIdr")
private String msgDefIdr;
@XmlElement(name = "BizSvc")
private String bizSvc;
@XmlElement(name = "CreDt")
private String creDt;
public FrEntity getFrEntity() {
return frEntity;
}
public void setFrEntity(FrEntity frEntity) {
this.frEntity = frEntity;
}
public ToEntity getToEntity() {
return toEntity;
}
public void setToEntity(ToEntity toEntity) {
this.toEntity = toEntity;
}
public String getBizMsgIdr() {
return bizMsgIdr;
}
public void setBizMsgIdr(String bizMsgIdr) {
this.bizMsgIdr = bizMsgIdr;
}
public String getMsgDefIdr() {
return msgDefIdr;
}
public void setMsgDefIdr(String msgDefIdr) {
this.msgDefIdr = msgDefIdr;
}
public String getBizSvc() {
return bizSvc;
}
public void setBizSvc(String bizSvc) {
this.bizSvc = bizSvc;
}
public String getCreDt() {
return creDt;
}
public void setCreDt(String creDt) {
this.creDt = creDt;
}
@Override
public String toString() {
return "{" +
"frEntity=" + frEntity.toString() +
", toEntity=" + toEntity.toString() +
", bizMsgIdr='" + bizMsgIdr + ''' +
", msgDefIdr='" + msgDefIdr + ''' +
", bizSvc='" + bizSvc + ''' +
", creDt='" + creDt + ''' +
'}';
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class FrEntity {
@XmlElement(name = "BICFI")
private String bicfi;
public String getBicfi() {
return bicfi;
}
public void setBicfi(String bicfi) {
this.bicfi = bicfi;
}
@Override
public String toString() {
return "{" +
"bicfi='" + bicfi + ''' +
'}';
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class ToEntity {
@XmlElement(name = "BICFI")
private String bicfi;
public String getBicfi() {
return bicfi;
}
public void setBicfi(String bicfi) {
this.bicfi = bicfi;
}
@Override
public String toString() {
return "{" +
"bicfi='" + bicfi + ''' +
'}';
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class DocumentEntity {
@XmlElement(name = "PmtId")
private List<PmtIdEntity> pmtIdEntityList;
public List<PmtIdEntity> getPmtIdEntityList() {
return pmtIdEntityList;
}
public void setPmtIdEntityList(List<PmtIdEntity> pmtIdEntityList) {
this.pmtIdEntityList = pmtIdEntityList;
}
@Override
public String toString() {
return "{" +
"pmtIdEntityList=" + pmtIdEntityList.toString() +
'}';
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class PmtIdEntity {
@XmlElement(name = "InstrId")
private String instrId;
@XmlElement(name = "EndToEndId")
private String endToEndId;
@XmlElement(name = "UETR")
private String uetr;
public String getInstrId() {
return instrId;
}
public void setInstrId(String instrId) {
this.instrId = instrId;
}
public String getEndToEndId() {
return endToEndId;
}
public void setEndToEndId(String endToEndId) {
this.endToEndId = endToEndId;
}
public String getUetr() {
return uetr;
}
public void setUetr(String uetr) {
this.uetr = uetr;
}
@Override
public String toString() {
return "{" +
"instrId='" + instrId + ''' +
", endToEndId='" + endToEndId + ''' +
", uetr='" + uetr + ''' +
'}';
}
}
javax.xml.bind.UnmarshalException: 意外的元素 (uri:"urn:swift:xsd:envelope", local:"Envelope")。所需元素为<{}Envelope>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:712)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:232)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:227)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:94)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1117)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:542)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:524)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:510)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3063)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:836)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:534)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1216)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:228)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:199)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:179)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:164)
at com.framework.jabx.filexml.TestFileXml.main(TestFileXml.java:30)
解决方案如下:
public class TestFileXml {
/* public static void main(String[] args) {
File file = new File("D:\学习记录\JDK\JavaX\测试解析XML.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(EnvelopeEntity.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
EnvelopeEntity entity = (EnvelopeEntity) unmarshaller.unmarshal(file);
System.out.println(entity.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}*/
public static void main(String[] args) {
File file = new File("D:\学习记录\JDK\JavaX\测试解析XML.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(EnvelopeEntity.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SAXSource saxSource = newSAxSource(file);
EnvelopeEntity entity = (EnvelopeEntity) unmarshaller.unmarshal(saxSource);
System.out.println(entity.toString());
} catch (JAXBException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 这里主要是忽略xml文件的命名空间
* @param file
* @return
* @throws FileNotFoundException
* @throws ParserConfigurationException
* @throws SAXException
*/
protected static SAXSource newSAxSource(File file) throws FileNotFoundException, ParserConfigurationException, SAXException {
FileReader fileReader = new FileReader(file);
SAXParserFactory saxParserFactory = SAXParserFactory.newDefaultInstance();
saxParserFactory.setNamespaceAware(false);
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
SAXSource saxSource = new SAXSource(xmlReader, new InputSource(fileReader));
return saxSource;
}
}
EnvelopeEntity{appHdrEntity={frEntity={bicfi='CITIUS33XXX'}, toEntity={bicfi='CITIUS33XXX'}, bizMsgIdr='TESTINGSIT00003', msgDefIdr='pacs.008.001.08', bizSvc='swift.cbprplus.01', creDt='2019-01-11T12:43:41.960+00:00'}, documentEntity={pmtIdEntityList=[{instrId='1-TESTINGSIT00003', endToEndId='1-KK04044506271305', uetr='1-174c245f-2682-4291-ad67-2a41e530cd27'}, {instrId='2-TESTINGSIT00003', endToEndId='2-KK04044506271305', uetr='2-174c245f-2682-4291-ad67-2a41e530cd27'}]}}
总结:
原文地址:https://blog.csdn.net/gelong_bokewang/article/details/128563106
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_26140.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。