xml字符串转字符串树

前端之家收集整理的这篇文章主要介绍了xml字符串转字符串树前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.StringReader;
  6. import java.util.Stack;
  7.  
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import javax.xml.parsers.ParserConfigurationException;
  11.  
  12. import org.w3c.dom.Document;
  13. import org.w3c.dom.Element;
  14. import org.w3c.dom.Node;
  15. import org.w3c.dom.NodeList;
  16. import org.xml.sax.InputSource;
  17. import org.xml.sax.SAXException;
  18.  
  19. /**
  20. *将xml字符串转成树结构,此类所在包下应该放置一个名为string的文件文件内容为xml格式字符串<br>
  21. *
  22. *
  23. 字符串<br>
  24. <operation_in><service_name></service_name><verify_code/><request_type/><sysfunc_id>10010082</sysfunc_id><trans_code/><operator_id/><organ_id>1000</organ_id><request_time/><request_seq/><request_source>100001</request_source><request_target>200001</request_target><msg_version>0100</msg_version><cont_version>0100</cont_version><content><field><name>ActionCode</name><id>0</id><simple>QUERYOFFER</simple></field><field><name>Mobile</name><id>0</id><simple>15210939334</simple></field><field><name>Source</name><id>0</id><simple>WEB3</simple></field><field><name>UserTime</name><id>0</id><simple>09/06/2015 16:58:24</simple></field><field><name>FIELD1</name><id>0</id><simple>111002000047</simple></field></content></operation_in>
  25. 的转换结果为<br>
  26. operation_in
  27. |
  28. |-------service_name······························<br>
  29. |
  30. |-------verify_code·······························<br>
  31. |
  32. |-------request_type······························<br>
  33. |
  34. |-------sysfunc_id································10010082<br>
  35. |
  36. |-------trans_code································<br>
  37. |
  38. |-------operator_id·······························<br>
  39. |
  40. |-------organ_id··································1000<br>
  41. |
  42. |-------request_time······························<br>
  43. |
  44. |-------request_seq·······························<br>
  45. |
  46. |-------request_source····························100001<br>
  47. |
  48. |-------request_target····························200001<br>
  49. |
  50. |-------msg_version·······························0100<br>
  51. |
  52. |-------cont_version······························0100<br>
  53. |
  54. |-------content<br>
  55. | |
  56. | |-------field<br>
  57. | | |
  58. | | |-------name······················ActionCode<br>
  59. | | |
  60. | | |-------id························0<br>
  61. | | |
  62. | | |-------simple····················QUERYOFFER<br>
  63. | |
  64. | |-------field<br>
  65. | | |
  66. | | |-------name······················Mobile<br>
  67. | | |
  68. | | |-------id························0<br>
  69. | | |
  70. | | |-------simple····················15210939334<br>
  71. | |
  72. | |-------field<br>
  73. | | |
  74. | | |-------name······················Source<br>
  75. | | |
  76. | | |-------id························0<br>
  77. | | |
  78. | | |-------simple····················WEB3<br>
  79. | |
  80. | |-------field<br>
  81. | | |
  82. | | |-------name······················UserTime<br>
  83. | | |
  84. | | |-------id························0<br>
  85. | | |
  86. | | |-------simple····················09/06/2015 16:58:24<br>
  87. | |
  88. | |-------field<br>
  89. | | |
  90. | | |-------name······················FIELD1<br>
  91. | | |
  92. | | |-------id························0<br>
  93. | | |
  94. | | |-------simple····················111002000047<br>
  95.  
  96. */
  97. public class Main {
  98.  
  99. final static String depthStr="| ";
  100. final static String nodeStr="|-------";
  101. final static String line="\n";
  102. final static char valueSpliter='·';
  103. final static int VALUE_START_POSITION=33;//调节显示节点值的位置
  104. public static void main(String[] args) throws IOException {
  105. //此类所在的包下有一个名叫string的文件,里面存放xml格式的报文
  106. InputStream stream=Main.class.getClassLoader().getResourceAsStream("cn/com/lewis/xml2tree/string");
  107. BufferedReader br=null;
  108. try {
  109. br = new BufferedReader(new InputStreamReader(stream,"GBK"));
  110. StringBuilder builder=new StringBuilder();
  111. String tmp=null;
  112. while((tmp=br.readLine())!=null){
  113. builder.append(tmp);
  114. }
  115. System.out.println(xmlToTree(builder.toString()));
  116. } catch (IOException | ParserConfigurationException | SAXException e1) {
  117. e1.printStackTrace();
  118. }finally{
  119. if(br!=null){
  120. br.close();
  121. }
  122. }
  123. }
  124. /**
  125. * 转换函数,非递归方式的实现
  126. * @param document
  127. * @return
  128. * @throws ParserConfigurationException
  129. * @throws IOException
  130. * @throws SAXException
  131. */
  132. public static String xmlToTree(String reqStr) throws ParserConfigurationException,SAXException,IOException{
  133. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  134. DocumentBuilder db = dbf.newDocumentBuilder();
  135. StringReader sr = new StringReader(reqStr);
  136. Document document=db.parse(new InputSource(sr));
  137. Element root=document.getDocumentElement();
  138. NodeList iterator=root.getChildNodes();
  139. Stack<StackItem<Integer>> stack=new Stack<StackItem<Integer>>();
  140. int j=0;
  141. StringBuilder builder=new StringBuilder(root.getNodeName()+line);
  142. Integer depth=0;//此处必须是对象,因为泛型要取匹配类型
  143. do{
  144. for(;j<iterator.getLength();j++){
  145. Node childNode = iterator.item(j);
  146. if(childNode.getNodeType() == Node.ELEMENT_NODE){
  147. StringBuilder tmpBuilder=new StringBuilder();
  148. for(int ii=0;ii<depth;++ii){
  149. builder.append(depthStr);
  150. tmpBuilder.append(depthStr);
  151. }
  152. builder.append(depthStr).append(line).append(tmpBuilder).append(nodeStr).append(childNode.getNodeName());
  153. if(childNode.getChildNodes().getLength()==0||
  154. (childNode.getChildNodes().getLength()==1&&childNode.getChildNodes().item(0).getNodeType() != Node.ELEMENT_NODE) ){
  155. int chc=depthStr.length()*depth+nodeStr.length()+childNode.getNodeName().length();
  156. for(int ii=0;ii<VALUE_START_POSITION-chc;++ii){
  157. builder.append(valueSpliter);
  158. }
  159. builder.append("示例值:").append(childNode.getTextContent());
  160. }else{
  161. stack.push(new StackItem<Integer>(iterator,j+1,depth));
  162. depth=new Integer(depth.intValue()+1);//防止编译时优化成对象自增
  163. j=-1;//j++后值变成0,获取下级节点的首子节点
  164. iterator=childNode.getChildNodes();
  165. }
  166. builder.append(line);
  167. }
  168. }
  169. if(stack.isEmpty()){
  170. break;
  171. }
  172. StackItem<Integer> topItem=stack.pop();
  173. iterator=topItem.iterator;
  174. j=topItem.index;
  175. depth=topItem.cacheObject;
  176. }while(true);
  177. return builder.toString();
  178. }
  179. /**
  180. *
  181. *
  182. * @param <T>
  183. */
  184. static class StackItem<T> {
  185. /**
  186. * 迭代到的xml节点
  187. */
  188. public NodeList iterator;
  189. /**
  190. * 迭代到的序号
  191. */
  192. public int index;
  193. /**
  194. * 附加的数据
  195. */
  196. public T cacheObject;
  197.  
  198. public StackItem(NodeList iterator,int index,T cacheObject) {
  199. super();
  200. this.iterator = iterator;
  201. this.index = index;
  202. this.cacheObject = cacheObject;
  203. }
  204.  
  205. public StackItem() {
  206. super();
  207. }
  208. }
  209. }

猜你在找的XML相关文章