Chi*_*ani 2 java xml hashmap xml-parsing java-8
我的xml看起来像:
<?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>
当我尝试在解析xml时填充hashmap时出现问题.我使用四个hashmap来保存不同级别的信息.因此,最终的hashmap包含来自较低级别的hashmap,例如setup,group和light,每个级别的属性是该级别的相应地图的键.
    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的hashmap也是如此.因此,当覆盖较低级别的hashmap时,先前的条目将丢失.
我理解hashmap中的"key"指向value的位置(这里是另一个hashmap).因此,混淆了,接下来可以做什么来检索所有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");
    }
    }
谢谢提前!!
我想你走错了路.如果你花时间真正地将数据解析为对象,那么我会创建一个真实的模型; 而不是使用字符串映射.你看,你目前的做法非常笨拙.那些地图地图不仅难以填充; 他们以后也很难用.虽然它不直接"可见" - 但您的XML数据是结构化的.并通过推说:"盲目"进入地图,你赚了很多该结构的更难获得.
我的意思是:在最低级别,你正在处理灯光.那你为什么不创建一个包含相应属性的Light 类呢?但不是原始字符串,而是已经转换为更具体的类型.然后你不会将Lights推入Map,而是将其推入List - 因为它们已经按顺序进入.
然后,您应该使用现有技术(如JAXB)将XML数据转换为"真实"对象 - 请参阅此处获取指导.