Java - 将XML文件属性添加到arraylist

时间:2017-12-13 02:40:48

标签: java xml arraylist

我有一个XML文件

<properties>
  <setting>
    <group>A</group>
    <name>John</name>
    <job>Manager</job>
  </setting>
  <setting>
    <group>B</group>
    <name>Peter</name>
    <job>Admin</job>
  </setting>
</properties>

接下来,我的代码将从URL读入,拆分文本并添加到arraylist

ArrayList<Employee> pArray = new ArrayList<SettingForm>();
try {
    URL url = new URL(urlLink);
    BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;

    while ((line= input.readLine()) != null){
        String[] value = line.split("=");
        if(value.length > 1){
            pArray .add(new Employee(value[0], value[1], group, name, job));
        }
    }
    input.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
}

将值[0]和值[1]添加到arraylist之后,我还需要接受group,name和job的XML属性并添加到arraylist中。我该怎么做才能实现这个目标?

1 个答案:

答案 0 :(得分:0)

您可以在javax.xml.parsers包中使用Java内置的XML解析器将XML加载到文档中,然后遍历NodeList以找到正确的属性。以数组形式将正确的属性添加到列表中,并在完成时返回列表的迭代器:

public static Iterator<String> getAttributes(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    ArrayList<String> attributes = new ArrayList<String>();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    /*
     * optional, but recommended read this -
     * http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with
     * -java-how-does-it-work
     */
    doc.getDocumentElement().normalize();

    NodeList nList = doc.getElementsByTagName("setting");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;

            String group = eElement.getElementsByTagName("group").item(0).getTextContent();
            String name = eElement.getElementsByTagName("name").item(0).getTextContent();
            String job = eElement.getElementsByTagName("job").item(0).getTextContent();

            attributes.add(group);
            attributes.add(name);
            attributes.add(job);
        }
    }
    return attributes.iterator();
}

现在,您只需在阅读网址之前致电getAttributes(),然后使用Iterator#next()以正确的顺序获取信息:

    Iterator<String> iterator;
    try {
        iterator = getAttributes(new File("..."));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
        return;
    }
    ArrayList<Employee> pArray = new ArrayList<SettingForm>();
    try {
        URL url = new URL(urlLink);
        BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;

        while ((line= input.readLine()) != null){
            String[] value = line.split("=");
            if(value.length > 1){
                pArray .add(new Employee(value[0], value[1], iterator.next(), iterator.next(), iterator.next()));
            }
        }
        input.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
    }