2016-12-04 71 views
2

我的XML是這樣的:當我嘗試填充的HashMap在解析XML出現填充從XML(JAVA)的HashMap

<?xml version="1.0"?> 
<Grid id = 1> 
    <Setup id = "1"> 
     <Group name = "DrawingRoom"> 
      <Light id = "1"> 
       <name>Light1</name> 
       <type>ben</type> 
       <index>1</index> 
      </Light> 
      <Light id = "2"> 
       <name>Light2</name> 
       <type>crux</type> 
       <index>2</index> 
      </Light> 
      <Light id = "3"> 
       <name>Light3</name> 
       <type>let</type> 
       <index>3</index> 
      </Light> 
     </Group> 
     <Group name = "BedRoom"> 
      <Light id = "1"> 
       <name>Light1</name> 
       <type>let</type> 
       <index>4</index> 
      </Light> 
     </Group> 
    </Setup> 
    <Setup id = "2"> 
     <Group name = "ClubRoom"> 
      <Light id = "1"> 
       <name>Light1</name> 
       <type>let</type> 
       <index>1</index> 
      </Light> 
     </Group> 
    </Setup> 
</Grid> 

問題。我使用四個hashmap來保存不同級別的信息。因此,最終的hashmap包含來自較低級別的hashmap,如設置,組和燈光,每個級別的屬性都是該級別各個映射的關鍵。

public HashMap<String,String> lightContent = new HashMap<String,String>(); 
    public HashMap<String, HashMap<String,String>> groupContent = new HashMap<String, HashMap<String,String>>(); 
    public HashMap<String, HashMap<String, HashMap<String,String>>> setupContent = new HashMap<String, HashMap<String, HashMap<String,String>>>(); 
    public HashMap<String, HashMap<String, HashMap<String, HashMap<String,String>>>> gridContent = new HashMap<String, HashMap<String, HashMap<String, HashMap<String,String>>>>(); 

在分析過程中,問題是隨着哈希映射得到更新,自動包含這些哈希映射的哈希映射。因此,當較低級別的hashmap被覆蓋時,以前的條目會丟失。

我知道hashmap中的「key」指向值的位置(這裏是另一個hashmap)。因此,m感到困惑,接下來可以做什麼來檢索所有的xml數據到hashmap。感謝您在這方面的幫助!

恐怕,不能使用任何外部庫來達到此目的。這裏是功能:

public void getSetupConfiguration() 
    { 
     try { 

      File fXmlFile = new File("D:\\SetupConfig.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile); 
      NodeList nodeList = doc.getChildNodes(); 


      if (doc.hasChildNodes()) 
       { 
       for (int count1 = 0; count1 < nodeList.getLength(); count1++) 
        { 
        System.out.println(nodeList.getLength()); 
       Node gridNode = nodeList.item(count1); 
       if (gridNode.getNodeType() == Node.ELEMENT_NODE) 
        { 
        String presentNodeName = gridNode.getNodeName(); 
        // get node name and value 
    //     System.out.println("\nNode Name =" + presentNodeName); 

        if (presentNodeName.equalsIgnoreCase("Grid")) 
         { 
         if (gridNode.hasChildNodes()) 
          { 
          Node setupNode = gridNode.getFirstChild(); 
          while (setupNode != null) 
           { 
           if (setupNode.getNodeType() == Node.ELEMENT_NODE) 
            { 
            System.out.println(setupNode.getNodeName()); 
            //Getting group 
            Node groupNode = setupNode.getFirstChild(); 
            while (groupNode != null) 
             { 
             if (groupNode.getNodeType() == Node.ELEMENT_NODE) 
              { 
               System.out.println(groupNode.getNodeName()); 
              // Getting lights 
              Node lightNode = groupNode.getFirstChild(); 
              while (lightNode != null) 
               { 
               if (lightNode.getNodeType() == Node.ELEMENT_NODE) 
                { 
                System.out.println(lightNode.getNodeName()); 
                // Getting individual lights 
                Node lights = lightNode.getFirstChild(); 
                while (lights != null) 
                 { 
                 if (lights.getNodeType() == Node.ELEMENT_NODE) 
                  { 
                  lightContent.put(lights.getNodeName(), lights.getTextContent()); 
                  System.out.println("aaa"); 
                  } 
                  lights = lights.getNextSibling(); 


                 } 
                NamedNodeMap lightMap = lightNode.getAttributes(); 
                String lightId = ""; 
                // To get id of Light element 
                for (int i = 0; i < lightMap.getLength(); i++) 
                {          
                 Node lightItem = lightMap.item(i); 
                 lightId = lightItem.getNodeValue(); 
                } 

                groupContent.put(lightId, lightContent); 
                System.out.println(groupContent); 

                } 

                lightNode = lightNode.getNextSibling(); 

               }// Populating Light Node Ends 
              NamedNodeMap groupMap = groupNode.getAttributes(); 
              String groupName = ""; 

              for (int i = 0; i < groupMap.getLength(); i++) 
              {          
               Node lightItem = groupMap.item(i); 
               groupName = lightItem.getNodeValue(); 
              } 
              setupContent.put(groupName, groupContent); 
              System.out.println(setupContent); 
              } 
              lightContent.clear(); 
              groupContent.clear(); 
              System.out.println(lightContent); 
              groupNode = groupNode.getNextSibling(); 
             } 
            } 
           if (setupNode.getNodeType() == Node.ELEMENT_NODE) 
           { 
            NamedNodeMap setupMap = setupNode.getAttributes(); 
            String setUpId = ""; 

            for (int i = 0; i < setupMap.getLength(); i++) 
            {          
             Node lightItem = setupMap.item(i); 
             setUpId = lightItem.getNodeValue(); 
            } 
            gridContent.put(setUpId, setupContent); 
            System.out.println(gridContent); 
            setupContent.clear(); 
           } 

           setupNode = setupNode.getNextSibling(); 
           } 

          } 
//       gridNode = gridNode.getNextSibling(); 

          } 
        } 
       } 
      } 
      System.out.println(gridContent); 
      } 
      catch (Exception e) 
      { 
      e.printStackTrace(); 
      } 



     System.out.println("Wow"); 
    } 
    } 

感謝提前!

回答

5

我想你會走錯路。如果您花時間真正將數據解析爲對象,那麼我會創建一個真實模型;而不是使用字符串地圖。你看,你現在的做法是極其笨拙極其。那些地圖貼圖不僅需要hard才能填充;他們也會在稍後使用。雖然它不是直接「可見的」 - 您的XML數據是結構化。通過將「盲目」推入地圖,您正在製作很多結構難以獲得的

我的意思是:在最底層,您正在處理燈光。那麼爲什麼不創建一個包含相應屬性的Light ?但不是原始字符串,而是已經轉換爲更具體的類型。然後,你不會將Lights推入地圖,而是將其放入列表 - 因爲它們已經按照該順序進入。

然後您應該使用現有的等技術(如JAXB)將XML數據轉換爲「真實」對象 - 有關指導,請參閱here