1

当我在元素节点上时,如何简化乱七八糟的代码?我的代码如下所示:

private void readXmldata(String xml_debug_settings)
{
    File xml_debug_settings_file = new File(xml_debug_settings);

    if (xml_debug_settings_file.exists())
    {            
        SAXReader saxReader = new SAXReader();

        try {

            Document document = saxReader.read(xml_debug_settings_file);

            Element root = document.getRootElement();

            Iterator itr = root.elements().iterator();  

            Element element =null;

            while (itr.hasNext()) {  

                Element debel = (Element) itr.next();

                if (debel.getName().equals("mainnode")) { 

                    Iterator itrd = debel.elementIterator();

                    while (itrd.hasNext())
                    {
                        Element child = (Element) itrd.next();

                        System.out.println(child.getName());

                        if (child.getName().equals("node1"))
                        {
                            System.out.println(child.getText());
                        }

                        if (child.getName().equals("node2"))
                        {
                            System.out.println(child.getText());
                        }

                        if (child.getName().equals("node3"))
                        {
                            System.out.println(child.getText());
                        }

                        if (child.getName().equals("node4"))
                        {
                            System.out.println(child.getText());
                        }

                        if (child.getName().equals("node4"))
                        {
                            System.out.println(child.getText());
                        }

                        if (child.getName().equals("node5"))
                        {
                            System.out.println(child.getText());
                        }
                    }

               }  

            }  

        } catch (DocumentException ex) {
            Logger.getLogger(Debugsettings.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

}

我想要这个例子的代码更少:(非常混乱并且没有正确清理代码):

    if (child.getName().equals("node1"))
    {
        System.out.println(child.getText());
    }

    if (child.getName().equals("node2"))
    {
        System.out.println(child.getText());
    }

    if (child.getName().equals("node3"))
    {
        System.out.println(child.getText());
    }

    if (child.getName().equals("node4"))
    {
        System.out.println(child.getText());
    }

    if (child.getName().equals("node4"))
    {
        System.out.println(child.getText());
    }

    if (child.getName().equals("node5"))
    {
        System.out.println(child.getText());
    }
4

1 回答 1

0

解决了。

所以,没有用户推荐我回答,所以我找到了解决方案。

public Map<String, String> readXmldata(String xml_debug_settings, String node)
    {

        File xml_debug_settings_file = new File(xml_debug_settings);

        if (xml_debug_settings_file.exists())
        {            
            SAXReader saxReader = new SAXReader();

            try {

                Document document = saxReader.read(xml_debug_settings_file);

                //Document temp = DocumentHelper.parseText("");  

                Element root = document.getRootElement();

                Iterator itr = root.elements().iterator();  

                Element element = null;

                while (itr.hasNext()) {  

                    Element debel = (Element) itr.next();

                    // System.out.println(debel.getName());    // node_name_1

                    if (debel.getName().equals(node)) { 

                        Iterator itrd = debel.elementIterator();

                        while (itrd.hasNext())
                        {
                            Element child = (Element) itrd.next();
                            String name = child.getName();  // node_name_2

                            int nodecount = child.nodeCount();

                            for (int i=0; i<nodecount; i++)
                            {
                                xmlarray.put(name, child.getStringValue());
                            }
                        }

                        return this.xmlarray;
                   }
                }  

            } catch (DocumentException ex) {
                Logger.getLogger(Debugsettings.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
        else
        {
            JOptionPane.showMessageDialog(null, "Not exists:"  + xml_debug_settings, "Failed to open xml", JOptionPane.WARNING_MESSAGE);
        }
        return null;

    }

并获取这样的数据:

Map<String, String> arrayList = xmlparser.readXmldata(this.xml_debug_settings, "someconfig");

System.out.prinln(arrayList.get("config1"));

xml结构:

<document>
 <someconfig>
   <config1>value_of_config</config>
 </someconfig>
</document>

希望有人帮忙。

于 2013-07-17T10:08:18.910 回答