2013-03-14 72 views
3

我想要兩個哈希表以下的XML。第一個是(屏幕ID,小工具ID) 和第二個是(小工具ID,字符串ID)。如何使用DOM解析此XML並將其內容放入散列表中?

我已經能夠使用DOM解析此XML,但將其內容放入哈希表是我沒有做的。

<?xml version="1.0" encoding="UTF-8"?> 

<screen id="616699" name ="SCR_NEW_HOME"> 

     <widget id="617259" type="label" name= "NEW_HOME_TITLE"> 
     <attribute type = "Strings"> 

      <val id="54">HOME_SYSSETUP</val> 
     </attribute> 
     </widget> 
     <widget id="616836" type = "label" name ="HOME_MENU"> 
     <attribute type="Strings"> 

       <val id="1815" >DAILY</val> 
       <val id="2060" >MONTH_NOV</val> 
       <val id="1221" >ASPECT_RATIO_PANSCAN</val> 
     </attribute> 
     </widget> 

<screen id="1556" name="SCR_EVENTLIST"> 

     <widget id="77009" type= "label" name="EL_GUIDE_EVENT_TABLE"> 
     <attribute type ="Strings"> 

       <val id="1">time</val> 
       <val id="2">date</val> 
     </attribute> 
     </widget> 
     <widget id="186461" type= "label" name= "EL_PIG_CONT"> 
     <attribute type ="Strings"> 

       <val id="3">progress bar</val> 
       <val id="4">video</val> 
     </attribute> 
    </widget> 

,我已經嘗試的代碼是

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.NamedNodeMap; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import org.w3c.dom.Element; 
import org.xml.sax.SAXException; 

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Hashtable; 

public class ReadXmlFile { 
    private static Hashtable<Integer,ArrayList<Integer>> D1 = new Hashtable<Integer, ArrayList<Integer>>(); 
    private static Hashtable<Integer,ArrayList<Integer>> D2 = new Hashtable<Integer,ArrayList<Integer>>(); 
    static Integer ScreenID; 
    static ArrayList<Integer> StringID; 
    static ArrayList<Integer> WidgetID; 
    static Integer WidgetID2; 
    public static void main(String argv[]) { 

    // try { 

    File fXmlFile = new File("E:/eclipse workspace/stringValidation/screens.xml"); 
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = null; 
    try { 
     dBuilder = dbFactory.newDocumentBuilder(); 
    } catch (ParserConfigurationException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    Document doc = null; 
    try { 
     doc = dBuilder.parse(fXmlFile); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    doc.getDocumentElement().normalize(); 


    System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
    if(doc.hasChildNodes()){ 

     printNote(doc.getChildNodes()); 

    } 


    } 
private static void printNote(NodeList nodeList) { 


     for (int count = 0; count < nodeList.getLength(); count++) { 

     Node tempNode = nodeList.item(count); 


     // make sure it's element node. 
     if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 

      // get node name and value 
      System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]"); 
      System.out.println("Node Value =" + tempNode.getTextContent()); 

      if (tempNode.hasAttributes()) { 

       // get attributes names and values 
       NamedNodeMap nodeMap = tempNode.getAttributes(); 

       for (int i = 0; i < nodeMap.getLength(); i++) { 

        Node node = nodeMap.item(i); 
        System.out.println("attr name : " + node.getNodeName()); 
        System.out.println("attr value : " + node.getNodeValue()); 


      } 

     } 

     if (tempNode.hasChildNodes()) { 

      // loop again if has child nodes 
      printNote(tempNode.getChildNodes()); 

     } 

     System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]"); 

    } 

    } 

    } 
} 
+0

請你能發佈所有相關代碼的問題嗎?大多數用戶可能不想或有時間編寫一個DOM解析器,以便解決「放入哈希表」的實際問題。 – andyb 2013-03-14 09:14:16

+4

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – dokaspar 2013-03-14 09:16:19

+0

我已經把這裏,無論我試過@andyb – Shalini 2013-03-21 14:27:19

回答

0

通過查看您的代碼,我假設您能夠打印出整個文檔,但不能找到您要查找的數據。我通過XMLBeam做類似的事情。它使您可以創建所需數據的面向對象表示,而無需遵循正在處理的xml的完整結構。以下是如何在你的第一個XML文件中提取數據,第二個是剛剛(不是通過手工DOM步行和更短的)一樣簡單:

public class TestFirst { 

@XBDocURL("res://first.xml") 
public interface Projection {    
    @XBRead("//screen") 
    List<Screen> getScreens(); 
} 

public interface Widget { 
    @XBRead("./@id") 
    String getID(); 

    @XBRead("./attribute[@type='Strings']/val") 
    List<String> getStringAttributes(); 
} 

public interface Screen {         
    @XBRead("./@id") 
    String getID(); 

    @XBRead("./widget") 
    List<Widget> getWidgets(); 
} 


@Test 
public void testFirst() throws IOException { 
    Projection projection = new XBProjector().io().fromURLAnnotation(Projection.class); 
    for (Screen screen:projection.getScreens()) { 
     for (Widget widget:screen.getWidgets()) { 
      for (String string:widget.getStringAttributes()) { 
       System.out.println(screen.getID()+" "+ widget.getID()+ " "+ string); 
      } 
     } 
    } 
} 

}

此打印出

616699 617259 HOME_SYSSETUP 
616699 616836 DAILY 
616699 616836 MONTH_NOV 
616699 616836 ASPECT_RATIO_PANSCAN 

現在你應該可以填寫你的HashTable了。 (PLZ考慮HashMap或ConcurrentHashMap)