当我在元素节点上时,如何简化代码的混乱?我的代码如下:
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());
}
答案 0 :(得分: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>
希望有人帮忙。