java中dom解析xml

从xml文件中得到某个节点中value的值,条件是已知道某一个子节点的参数,如下一片段,

已知 <name> 为 “Motor hand”的值,想从整个xml文件中得到此子节点的<value>所对应的值。

“<field>
<name>Motor hand</name>
<value>Right</value>
<type>Dotted</type>
</field>”

此方法是dom 遍历,获取大的节点并解析出子节点并属性和值一起存入到map中,将所有大节点再加入到链表中以备之后的其他程序调用(另附,从文件读取xml文件的方法):

 1 public static void main(String[] args) 

 2     {
//"
strxml"是 xml 转的string, 是个完整的xml对应的字符串,
3
String result=findbynode(strxml,"field","name","Motor hand","value");

4 System.out.print(result);

5 //得到的结果是“Right”

6
 7          

 8  }  9  

10 /** 11  * common method "findbynode" get target from Xml 2 12  * 13  * @param args example: 14  * 15  * { 16  * <field> //field-->bignode 17  * <name>Motor hand</name> //name-->clum1 , "Motor hand"-->itemvalue 18  * <value>Right</value> //value-->clum2 19  * <type>Dotted</type> 20  * </field> 21  * } 22  * 23  */

24 public static String findbynode(String passxmlstr,String bignode,String clum1,String itemvalue,String clum2) 25 { 26     String result=""; 27     

28     /*

29  bignode="field"; 30  clum1="name"; 31  clum2="value"; 32     */

33     

34     ArrayList<Map> al=paserxmlbynode(passxmlstr,bignode); 35     for(Map m : al) 36  { 37         if(m.containsKey(clum1)&&m.get(clum1).toString().equals(itemvalue)) 38  { 39              result=m.get(clum2).toString(); 40              break; 41  } 42  } 43     //System.out.println(result);

44     return result; 45 } 46 

47 public static ArrayList paserxmlbynode(String passxmlstr,String bignode) 48 { 49      Document xmlDoc=null; 50      

51      try { 52             ByteArrayInputStream bis = new ByteArrayInputStream(passxmlstr.getBytes("UTF-8")); 53             xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bis); 54         } catch (Throwable e) { 55             try { 56                 throw new Exception("MSG_COULDN'T_PARSE_XML", e); 57             } catch (Exception e1) { 58                 // TODO Auto-generated catch block

59  e1.printStackTrace(); 60  } 61  } 62         

63          // System.out.print(passxmlstr);

64         ArrayList<Map> listitem=new ArrayList<Map>(); 65         

66         // find bignode

67         NodeList nlds=xmlDoc.getElementsByTagName(bignode); 68         int size =nlds.getLength(); 69         for(int j=0;j<size;j++) 70  { 71             Node nd=nlds.item(j).getFirstChild().getNextSibling(); 72             Map<String,String> map=new HashMap<String,String>(); 73             while(nd!=null) 74  { 75  map.put(nd.getNodeName(), nd.getFirstChild().getNodeValue()); 76                 nd= nd.getNextSibling().getNextSibling(); 77  } 78  listitem.add(map); 79  } 80       

81        // System.out.print(listitem.size());

82      return listitem; 83 } 84    

附:从文件读取xml文件

 1     public String getStringofxmlfromdisk()

 2     {

 3         String passxmlstr = "";

 4         String filepath="D:\\test\\rxml.xml";

 5  

 6         BufferedReader reader = null;

 7         try {

 8             reader = new BufferedReader(new FileReader(new File(filepath)));

 9             String tempString = null;

10             int line = 1;

11             while ((tempString = reader.readLine()) != null) {

12                 passxmlstr += tempString;

13                 line++;

14             }

15             // System.out.println("  " + passxmlstr);

16             reader.close();

17         } catch (IOException e) {

18             e.printStackTrace();

19         } finally {

20             if (reader != null) {

21                 try {

22                     reader.close();

23                 } catch (IOException e1) {

24                 }

25             }

26         }

27         return passxmlstr;

28     }

 

你可能感兴趣的:(dom解析xml)