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

  • 相关阅读:
    Week14
    带你一文读懂Batch Normalization
    019_04简易相机
    019_03camera的调用
    019_02视频播放之VideoView
    019_01播放视频之SurfaceView
    018_04音效播放之MediaPlayer与SoundPool
    018_03简易美图
    018_02图片水印
    018_01图片特效
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016569.html
Copyright © 2011-2022 走看看