org.dom4j.DocumentException: Error on line 41 of document : 元素型別 “SPBMJC“ 必須由匹配的結束標記 “</SPBMJC>“ 終止
阿新 • • 發佈:2021-02-06
技術標籤:報錯
xml檔案解析的時候報錯
org.dom4j.DocumentException: Error on line 41 of document : 元素型別 "SPBMJC" 必須由匹配的結束標記 "</SPBMJC>" 終止。 Nested exception: 元素型別 "SPBMJC" 必須由匹配的結束標記 "</SPBMJC>" 終止。
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j. io.SAXReader.read(SAXReader.java:343)
at com.taxchina.invoice.service.impl.taxbase.TaxBaseClassificCodeServiceImpl.importHand(TaxBaseClassificCodeServiceImpl.java:242)
at com.taxchina.invoice.service.impl.taxbase.TaxBaseClassificCodeServiceImpl$$FastClassBySpringCGLIB$$70de8664.invoke(<generated> )
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:750)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163 )
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.taxchina.invoice.service.impl.taxbase.TaxBaseClassificCodeServiceImpl$$EnhancerBySpringCGLIB$$bbc0e818.importHand(<generated>)
at com.taxchina.invoice.controller.taxbase.TaxBaseClassificCodeController.importHand(TaxBaseClassificCodeController.java:77)
at com.taxchina.invoice.controller.taxbase.TaxBaseClassificCodeController$$FastClassBySpringCGLIB$$a368110a.invoke(<generated>)
直接給提供了一個xml檔案所以沒有懷疑過檔案正確性,程式碼看著也沒啥問題,但是執行就報錯(上述),所以線上xml工具校驗了一下格式沒問題,不死心自己由檢查了一下所述的SPBMJC
標籤是閉合的沒問題,最終還是檢查了一下檔案
驚喜的發現宣告的編碼與檔案儲存編碼不一致,真坑,最後都改成utf-8就對了,我。。。
xml檔案解析程式碼
@Override
public String importHand(MultipartFile file) {
byte[] byteArr = new byte[0];
try {
byteArr = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputStream);
Element rootElement = document.getRootElement();
getNodes(rootElement);
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
return null;
}
public static void getNodes(Element rootElement) {
String name = rootElement.getName();
System.out.println("節點名稱:"+name);
//獲取節點屬性
List<Attribute> attributes = rootElement.attributes();
for (Attribute attribute:attributes) {
System.out.println("屬性名稱"+attribute.getName()+"---"+attribute.getText());
}
//獲取值
String value = rootElement.getTextTrim();
if(!value.equals("")) {
System.out.println("節點屬性名稱:"+value);
}
//判斷是否還有下個節點
Iterator<Element> elementIterator = rootElement.elementIterator();
while (elementIterator.hasNext()) {
Element next = elementIterator.next();//拿到下個節點
getNodes(next);
}