XML解析总结
1:XML和javaBean之间的映射
只要总结了两种方式:
1.1:XMap
the third support : nuxeo-common-5.4.3.jar(latest)
主要的操作对象:
org.nuxeo.common.xmap.XMap;
用法示例:
??? ??? List<UPCallcenterMenuTab> menuList = null;
??? ??? XMap xmap = new XMap();
??? ??? xmap.register(UPCallcenterMenuTabList.class);
??? ??? InputStream in = new FileInputStream(new File(filePath));
??? ??? Object[] result = (Object[]) xmap.loadAll(in);
??? ??? UPCallcenterMenuTabList menuTabList = (UPCallcenterMenuTabList) result[0];
??? ??? menuList = menuTabList.getMenuList();
1.2:XStream
the third support :xstream-1.4.1.jar
主要的操作对象:
com.thoughtworks.xstream.XStream
用法示例:
/****
??? ?* read operation
??? ?*
??? ?* @param filePath
??? ?*??????????? :specified file path
??? ?* @throws IOException
??? ?*/
??? @SuppressWarnings("unchecked")
??? private static List<UPCallcenterMenuTab> convertXMLToList(String filePath)??? ??? ??? {
??? ??? List<UPCallcenterMenuTab> menuList = null;
??? ??? if (StringUtils.isEmpty(filePath) || StringUtils.isBlank(filePath)) {
??? ??? ??? throw new NullPointerException("filePath is invalid!");
??? ??? }
??? ??? // 1:XStream instance
??? ??? XStream xstream = new XStream();
??? ??? xstream.alias("menus", List.class);
??? ??? xstream.alias("menu", UPCallcenterMenuTab.class);
??? ??? // 2:used to read bytes from file
??? ??? //FileReader fr = new FileReader(filePath);
???? ??? InputStreamReader reader = null;
??? ??? try {
??? ??? ??? reader = new InputStreamReader(new FileInputStream(filePath),"UTF-8");
??? ??? } catch (UnsupportedEncodingException e1) {
??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? e1.printStackTrace();
??? ??? } catch (FileNotFoundException e1) {
??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? e1.printStackTrace();
??? ??? }
??? ??? // buffer reader
??? ??? BufferedReader br = new BufferedReader(reader);
??? ??? // 3:construct ObjcetInputStream instance
??? ??? // ObjectInputStream ois = xstream.createObjectInputStream(br);
??? ??? // 4: read data using reader
??? ??? try {
??? ??? ??? menuList = (List<UPCallcenterMenuTab>) xstream.fromXML(br);
??? ??? ??? // menuList = (List<UPCallcenterMenuTab>)ois.readObject();
??? ??? ??? // } catch (ClassNotFoundException e) {
??? ??? } catch (Exception e) {
??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? logger.info("Excetpion ocurred when converting xml data to object");
??? ??? ??? logger.info(e.getMessage(),e);
??? ??? ??? //throw e;
??? ??? }
??? ??? return menuList;
??? }
1:3:XMLConfiguration
the third support:? commons-configuration-1.3.jar
主要的操作类:org.apache.commons.configuration
用法示例:
public final class Configuration {
??? private static Logger log = Logger.getLogger(Configuration.class);
??? private static Configuration configuration;
???
??? private XMLConfiguration config;
???
??? private Configuration(){
??? ??? try{
??? ??? ??? config = new XMLConfiguration();
??? ??? ??? config.setDelimiterParsingDisabled(true);??? ??? ???
??? ??? ??? config.load("lokosuite.cfg.xml");
??? ??? }catch(Exception e){
??? ??? ??? log.error(this,e);
??? ??? }
??? }
???
??? public static Configuration getInstance(){
??? ??? if(configuration == null){
??? ??? ??? configuration = new Configuration();
??? ??? }
??? ??? return configuration;
??? }
???
??? public String getString(String key){
??? ??? return config.getString(key);
??? }
???
??? public static void main(String[] args) throws Exception{
??? ??? String text= Configuration.getInstance().getString("membership.active-text");
??? ??? System.out.println(Configuration.getInstance().getString("shopping.transSuccess-text"));
??? ??? System.out.println(Configuration.getInstance().getString("membership.active-subject"));
??? ??? System.out.println(StringUtils.replace(text,"{activate-url}","adfasdf"));
??? ??? System.out.println(Configuration.getInstance().getString("core.version"));
??? ???
??? ???
??? }
}
继续:
1.4:xml文件和xml字符串之间的转换
<操作前:dom4j-1-6-1.jar 和 jaxen-1.1-beta-6.jar是必须的;附件中有这两个jar包>
/**
??? ??? ?* 在运行操作xpath的程序中 添加jaxen-1.1-beta-6.jar
??? ??? ?* 以防抛java.lang.NoClassDefFoundError: org/jaxen/JaxenException异常
?*/
1.4.1:xml文件转换为xml字符串
String xmlFileName = "你的文件绝对路径";
SAXReader? reader = new SAXReader();
FileInputStream fileInputStream = new FileInputStream(xmlFileName );
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fileInputStream ,"UTF-8"));
Document document = reader.read(bufferReader);
?
String xmlString = document.asXML();
1.4.2:xml字符串转换为xml文件
?
String xmlString = "<?xml version="1.0" encoding="UTF-8"?><book>text</book>";
?
SAXReader reader = new SAXReader(false);
reader.setEncoding("UTF-8");
??? ??? try {
??? ??? ??? ByteArrayInputStream byteStream = new ByteArrayInputStream(xmlString.getBytes());
??? ???
??? ??? ??? Document document = reader.read(byteStream);
? ? ? ? ? ? String fullFileName = "你要生成的完整的xml文件路径";
?? ? ? ? ?? File newFile = new File(fullFileName );
??? ? ? ? ? Writer writer = new FileWriter(newFile);
?? ??? ??? // 格式化输出
?? ?? ? ? OutputFormat format = OutputFormat.createPrettyPrint();
?? ?? ? ? // 设置编码
?? ??? ?? format.setEncoding("UTF-8");
?? ?? ??? XMLWriter xmlWriter = new XMLWriter(writer, format);
??? ??? ?? // String xPathString = "//book";
??? ??? ?? // Element element = (Element)document.selectSingleNode(xPathString);
??? ??? ?? // System.err.println(element.getText());
??? ??? } catch (DocumentException e) {
??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? e.printStackTrace();
??? ??? }
/********************************************************暂时告一段落!********************************************************/
待续。。。
?