使用java来解析几个父项的xml

时间:2015-05-30 18:33:26

标签: java xml

我是java和解析的新手,并研究了所有简单的例子。它们给了我基础知识,但是我如何解析一个更高级的xml文件,如下所示。

<?xml version="1.0"?>
<employee name="carlos">
  <employment_form works="fulltime">
     <employment_number>555</employment_number>
     <days_worked>200</days_worked>
     <employment_deps dependencies="started">
        <Another_Employee NAME="jenny"/>
        <Another_Employee NAME="jimmy"/>
     </employment_deps>
     <employment_deps dependencies="now">
        <Another_Employee NAME="jenny"/>
        <Another_Employee NAME="jimmy"/>
        <Another_Employee NAME="lisa"/>
     </employment_deps>
  </employment_form>
  <employment_form works="parttime">
     <employment_number>554</employment_number>
     <days_worked>21</days_worked>
     <employment_deps dependencies="started">
        <Another_Employee NAME="mike"/>
        <Another_Employee NAME="donny"/>
        <Another_Employee NAME="fredrik"/>
     </employment_deps>
     <employment_deps dependencies="when finished">
        <Another_Employee NAME="trish"/>
        <Another_Employee NAME="carl"/>
        <Another_Employee NAME="noone"/>
     </employment_deps>
    </employment_form>
   </employee>

我想显示每个employment_form的详细列表;

carlos fulltime 555 200 started
jenny
jimmy
carlos fulltime 555 200 now
jenny
jimmy
lisa
carlos parttime 554 21 started
mike
donny
fredrik
carlos parttime 554 21 now
trish
carl
noone

我希望有些人可以帮助我,我不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

您可以简单地使用java dom解析器来解析您的xml文档。

产生所需结果的粗略例子是

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

public class XmlParser {

    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            StringBuilder output = new StringBuilder();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new File("xmlfile"));
            Element root = doc.getDocumentElement();
            String employeeName = root.getAttribute("name");
            NodeList employmentForm = doc.getElementsByTagName("employment_form");

            for (int i = 0; i < employmentForm.getLength(); i++) {
                Node node = employmentForm.item(i);
                NamedNodeMap attribute = node.getAttributes();
                String works = attribute.getNamedItem("works").getNodeValue();

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String employeeNumber = element.getElementsByTagName("employment_number").item(0).getTextContent();
                    String daysWorked = element.getElementsByTagName("days_worked").item(0).getTextContent();
                    NodeList employmentDeps = element.getElementsByTagName("employment_deps");
                    String dependencies = employmentDeps.item(0).getAttributes().getNamedItem("dependencies").getNodeValue();

                    for (int j = 0; j < employmentDeps.getLength(); j++) {
                        output.append(employeeName).append(" ")
                              .append(works).append(" ")
                              .append(employeeNumber).append(" ")
                              .append(daysWorked).append(" ")
                              .append(dependencies).append("\n");
                        Node anotherEmployee = employmentDeps.item(j);
                        if (anotherEmployee.getNodeType() == Node.ELEMENT_NODE) {
                            Element anotherEmployeeElement = (Element) anotherEmployee;
                            NodeList anotherEmployeeList = anotherEmployeeElement.getElementsByTagName("Another_Employee");
                            for (int k = 0; k < anotherEmployeeList.getLength(); k++) {
                                Node anotherEmployeeNode = anotherEmployeeList.item(k);
                                if (anotherEmployeeNode.getNodeType() == Node.ELEMENT_NODE) {
                                output.append(anotherEmployeeNode.getAttributes().getNamedItem("NAME").getNodeValue()).append("\n");
                            }
                        }

                    }
                }
            }
        }

        System.out.println(output.toString());

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }
}