xml原文文件内容如下:
CITIUS33XXX
CITIUS33XXX
TESTINGSIT00003
pacs.008.001.08
swift.cbprplus.01
2019-01-11T12:43:41.960+00:00
1-TESTINGSIT00003
1-KK04044506271305
1-174c245f-2682-4291-ad67-2a41e530cd27
2-TESTINGSIT00003
2-KK04044506271305
2-174c245f-2682-4291-ad67-2a41e530cd27
使用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 pmtIdEntityList;
public List getPmtIdEntityList() {
return pmtIdEntityList;
}
public void setPmtIdEntityList(List 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'}]}}
总结:
我们可以使用javax.xml.parsers包下的功能类去解决读取xml文件中的命名空间问题。