/**
* 以字符串形式读取xml文档
* @param XMLString String 包含xml文档内容的字符串
* @throws XmlOperationException 抛出自定义的xml操作异常
* @return Document 返回读取结果的文档
*/
public static Document readXmlDocument(String XMLString) throws
XmlOperationException {
return readXmlDocument(XMLString, new Boolean(true));
}
/**
* 以字符串形式读取xml文档
* @param XMLString String 包含xml文档内容的字符串
* @param validate Boolean 是否校验标识
* @throws XmlOperationException 抛出自定义的xml操作异常
* @return Document 返回读取结果的文档
*/
public static Document readXmlDocument(String XMLString, Boolean validate) throws
XmlOperationException { if ("".equals(checkEmpty(XMLString))) {
LogUtil.log("[XML] 将XML字符串读入document时,XMLString为空或为“”。" +
"在XMLUtil方法readXmlDocument方法中。",
LogLevel.WARN);
return null;
} ByteArrayInputStream bis = null; try { bis = new ByteArrayInputStream(XMLString.getBytes("UTF-8"));
}
catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); LogUtil.log("[XML] 将XML字符串读入document时发生不支持字符集异常。" +
"在XMLUtil方法readXmlDocument方法中。异常信息:" +
e.getMessage(),
LogLevel.ERROR);
throw new XmlOperationException(
"将XML字符串读入document时发生不支持字符集异常。" +
"在XMLUtil方法readXmlDocument方法中。", e); }
return readXmlDocument(bis, validate, "Internal Content");
}
* 以字符串形式读取xml文档
* @param XMLString String 包含xml文档内容的字符串
* @throws XmlOperationException 抛出自定义的xml操作异常
* @return Document 返回读取结果的文档
*/
public static Document readXmlDocument(String XMLString) throws
XmlOperationException {
return readXmlDocument(XMLString, new Boolean(true));
}
/**
* 以字符串形式读取xml文档
* @param XMLString String 包含xml文档内容的字符串
* @param validate Boolean 是否校验标识
* @throws XmlOperationException 抛出自定义的xml操作异常
* @return Document 返回读取结果的文档
*/
public static Document readXmlDocument(String XMLString, Boolean validate) throws
XmlOperationException { if ("".equals(checkEmpty(XMLString))) {
LogUtil.log("[XML] 将XML字符串读入document时,XMLString为空或为“”。" +
"在XMLUtil方法readXmlDocument方法中。",
LogLevel.WARN);
return null;
} ByteArrayInputStream bis = null; try { bis = new ByteArrayInputStream(XMLString.getBytes("UTF-8"));
}
catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); LogUtil.log("[XML] 将XML字符串读入document时发生不支持字符集异常。" +
"在XMLUtil方法readXmlDocument方法中。异常信息:" +
e.getMessage(),
LogLevel.ERROR);
throw new XmlOperationException(
"将XML字符串读入document时发生不支持字符集异常。" +
"在XMLUtil方法readXmlDocument方法中。", e); }
return readXmlDocument(bis, validate, "Internal Content");
}
解决方案 »
- MultipartRequestWrapper RequestFacade 得到的session不一样 求解决方法
- struts2和spring整合问题(导入了struts-spring-plugin.jar)
- tomcat启动时,报:“严重:Error reading tld listeners java.io.EOFException”错误
- getHibernateTemplate 是否用 释放数据库连接? 我们网站 总当机 我在找原因 我们对数据库操作都是用 getHibernateTemplate
- hibernate 统一事务管理 批量SQL统一提交问题
- 关于Spring的超菜的问题。
- .jsp必须放在tomcat目录下才能运行吗?
- Bluej是什么
- 各位请帮忙,如何在WEBLOGIC下布置一个具体EJB项目,(即JSP调EJB).
- 如何插入数据库中这样的"★"字符。jsp字符及中文处理方面真差劲!
- 讨论:J2EE 是不是真的好多.NET?
- quartz和spring的配置问题!!
import java.io.*;
import java.util.*;import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.*;
import com.zcjy.framework.common.log.*;
import com.zcjy.framework.common.xml.*;public class TestJDomXMLStringRead { private static Document readXMLDocument(String XMLString) { Document document = null;
ByteArrayInputStream bais = null; try {
bais = new ByteArrayInputStream(XMLString.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} if (bais != null) { try {
SAXBuilder saxBuilder = new SAXBuilder();
document = saxBuilder.build(bais);
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (JDOMException ex) {
ex.printStackTrace();
}
}
else {
throw new NullPointerException("输入流为空!");
} return document;
} public static void main(String[] args) { String testXMLString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entitys><relations>" +
"<relation relationName=\"class_student\" relationType=\"one-to-many\"><pkEntity name=\"class\">" +
"<fields><field name=\"hh\"/><field name=\"id\"/></fields></pkEntity><fkEntity name=\"student\">" +
"<fields><field name=\"id\"/><field name=\"bb\"/></fields></fkEntity></relation></relations></entitys>";
Document document = readXMLDocument(testXMLString); System.out.println(document.toString());
}
}