烦:org.dom4j.DocumentException: Error on line 5 of document.....

10 篇文章 0 订阅
2 篇文章 0 订阅

背景要求:
DB2数据库字符集GBK,某字段类型BLOB用于存放xml格式内容的菜单树结构,其中存在中文。通过java读取其内容,对其进行增加或者删除节点后再保存到数据表中。

#字符集编码
InitParamUtil.PORTAL_DB_CHARSET=GBK

主要读取程序:
SqlMapClient sqlMap = null;                   
try                                       
{                              
 sqlMap = IbatisPortalFactory.getSqlMap();
 sqlMap.startTransaction();
 Reader strReader=new StringReader(new String(queryNodeTree(sqlMap).getNodeTree(), InitParamUtil.PORTAL_DB_CHARSET));
 SAXReader reader=new SAXReader();  
 Document doc=reader.read(strReader);
 for(int i=0;i<funcids.length;i++)
 { 
  Element elmt_=(Element)doc.selectSingleNode("//menu[@id=\""+funcids[i]+"\"]");
  if(elmt_!=null)
  {
   String id_  = elmt_.attributeValue("id");
   String sysid_  = elmt_.attributeValue("sysid");
   String type_   = elmt_.attributeValue("type");
    
     //主要增加删除操作省略

主要写入程序:
OutputFormat oFormat=OutputFormat.createPrettyPrint();
oFormat.setEncoding(InitParamUtil.PORTAL_DB_CHARSET);
StringWriter strWriter=new StringWriter();
XMLWriter output = new XMLWriter(strWriter,oFormat);
output.write(doc);
output.close();
PublishFuncInfoBean funcInfoBean2=new PublishFuncInfoBean();
funcInfoBean2.setNodeTree(strWriter.toString().getBytes(InitParamUtil.PORTAL_DB_CHARSET));
funcInfoBean2.setRemark("");

//数据库插入操作省略

出现异常:
org.dom4j.DocumentException: Error on line 5 of document  : The value of attribute "name" associated with an element type "null" must not contain the '<' character. Nested exception: The value of attribute "name" associated with an element type "null" must not contain the '<' character.
 at org.dom4j.io.SAXReader.read(SAXReader.java:350)
 at org.dom4j.io.SAXReader.read(SAXReader.java:274)
 at com.linkage.querytool.CleanPublishUtil.delMenu(CleanPublishUtil.java:83)
 at com.linkage.querytool.CleanPublishUtil.deleteFuncList(CleanPublishUtil.java:40)
 at com.linkage.querytool.EverydayUpdateThread.expiredPublishReportClean(EverydayUpdateThread.java:738)
 at com.linkage.querytool.EverydayUpdateThread$EverydayUpdateTask.run(EverydayUpdateThread.java:121)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)
Nested exception:
org.xml.sax.SAXParseException: The value of attribute "name" associated with an element type "null" must not contain the '<' character.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:932)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanAttribute(XMLNSDocumentScannerImpl.java:460)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:277)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
 at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
 at org.dom4j.io.SAXReader.read(SAXReader.java:334)
 at org.dom4j.io.SAXReader.read(SAXReader.java:274)
 at com.linkage.querytool.CleanPublishUtil.delMenu(CleanPublishUtil.java:83)
 at com.linkage.querytool.CleanPublishUtil.deleteFuncList(CleanPublishUtil.java:40)
 at com.linkage.querytool.EverydayUpdateThread.expiredPublishReportClean(EverydayUpdateThread.java:738)
 at com.linkage.querytool.EverydayUpdateThread$EverydayUpdateTask.run(EverydayUpdateThread.java:121)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)
Nested exception: org.xml.sax.SAXParseException: The value of attribute "name" associated with an element type "null" must not contain the '<' character.
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
 at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)
 at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:932)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanAttribute(XMLNSDocumentScannerImpl.java:460)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:277)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
 at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
 at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
 at org.dom4j.io.SAXReader.read(SAXReader.java:334)
 at org.dom4j.io.SAXReader.read(SAXReader.java:274)
 at com.linkage.querytool.CleanPublishUtil.delMenu(CleanPublishUtil.java:83)
 at com.linkage.querytool.CleanPublishUtil.deleteFuncList(CleanPublishUtil.java:40)
 at com.linkage.querytool.EverydayUpdateThread.expiredPublishReportClean(EverydayUpdateThread.java:738)
 at com.linkage.querytool.EverydayUpdateThread$EverydayUpdateTask.run(EverydayUpdateThread.java:121)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)

 

后通过调试发现问题出现在字符集上,默认使用InitParamUtil.PORTAL_DB_CHARSET,java从数据库中读取的菜单树内容中文乱码导致xml格式存在问题,于是修改
InitParamUtil.PORTAL_DB_CHARSET=UTF-8
程序正常!!!!!!!!!!!!!!!!

该问题主要是由字符集编码导致xml内容中的中文成为乱码,而进一步导致xml文件的格式出现问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值