zoukankan      html  css  js  c++  java
  • string 转化xml && xml转化为string

    一、使用最原始的javax.xml.parsers,标准的jdk api 

     

    // 字符串转XML 

     

    1. String xmlStr = "......";    
    2.   
    3. StringReader sr = new StringReader(xmlStr);   
    4.   
    5. InputSource is = new InputSource(sr);   
    6.   
    7. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
    8.   
    9. DocumentBuilder builder=factory.newDocumentBuilder();    
    10.   
    11. Document doc = builder.parse(is);    


    //XML转字符串 

    1. TransformerFactory  tf  =  TransformerFactory.newInstance();    
    2.   
    3. Transformer t = tf.newTransformer();    
    4.   
    5. t.setOutputProperty("encoding","GB23121");//解决中文问题,试过用GBK不行   
    6.   
    7. ByteArrayOutputStream  bos  =  new  ByteArrayOutputStream();   
    8.   
    9. t.transform(new DOMSource(doc), new StreamResult(bos));    
    10.   
    11. String xmlStr = bos.toString();    

    这里的XML DOCUMENT为org.w3c.dom.Document 

     

      二、使用dom4j后程式变得更简单 

     

    1. // 字符串转XML    
    2.   
    3. String xmlStr = "......";    
    4.   
    5. Document document = DocumentHelper.parseText(xmlStr);    
    6.   
    7.     
    8.   
    9. // XML转字符串    
    10.   
    11. Document document = ...;    
    12.   
    13. String text = document.asXML();    
    14.   
    15.     
    16.   
    17. 这里的XML DOCUMENT为org.dom4j.Document    

      三、使用JDOM 

     

    JDOM的处理方式和第一种方法处理很类似 

     

    1. //字符串转XML    
    2.   
    3. String xmlStr = ".....";    
    4.   
    5. StringReader sr = new StringReader(xmlStr);   
    6.   
    7. InputSource is = new InputSource(sr);   
    8.   
    9. Document doc = (new SAXBuilder()).build(is);   
    10.   
    11.     
    12.   
    13. //XML转字符串    
    14.   
    15. Format format = Format.getPrettyFormat();    
    16.   
    17. format.setEncoding("gb2312");//配置xml文档的字符为gb2312,解决中文问题   
    18.   
    19. XMLOutputter xmlout = new XMLOutputter(format);   
    20.   
    21. ByteArrayOutputStream bo = new ByteArrayOutputStream();   
    22.   
    23. xmlout.output(doc,bo);    
    24.   
    25. String xmlStr = bo.toString();    
    26.   
    27.     
    28.   
    29. 这里的XML DOCUMENT为org.jdom.Document    

     四、JAVASCRIPT中的处理 

     

    [javascript] view plaincopyprint?
    1. //字符串转XML    
    2.   
    3. var xmlStr = ".....";    
    4.   
    5. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");   
    6.   
    7. xmlDoc.async=false;    
    8.   
    9. xmlDoc.loadXML(xmlStr);    
    10.   
    11. //能够处理这个xmlDoc了    
    12.   
    13. var name = xmlDoc.selectSingleNode("/person/name");   
    14.   
    15. alert(name.text);    
    16.   
    17.     
    18.   
    19. //XML转字符串    
    20.   
    21. var xmlDoc = ......;    
    22.   
    23. var xmlStr = xmlDoc.xml    
    24.   
    25.     
    26.   
    27. 这里的XML DOCUMENT为javascript版的XMLDOM     

      string 转化xml xml转化为string

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016569.html
Copyright © 2011-2022 走看看