java代码解析部分xml文件

时间:2015-10-07 10:56:56

标签: java xml

我有一个xml文件:

<db>
<employees name="aaa">
  <employee id="111">
    <firstName>Rakesh</firstName>
    <lastName>Mishra</lastName>
    <location>Bangalore</location>
  </employee>
  <employee id="112">
    <firstName>John</firstName>
    <lastName>Davis</lastName>
    <location>Chennai</location>
  </employee>
  <employee id="113">
    <firstName>Rajesh</firstName>
    <lastName>Sharma</lastName>
    <location>Pune</location>
  </employee>
</employees>

<employees name="bbb">
  <employee id="222">
    <firstName>a</firstName>
    <lastName>a</lastName>
    <location>a</location>
  </employee>
  <employee id="223">
    <firstName>s</firstName>
    <lastName>s</lastName>
    <location>s</location>
  </employee>
  <employee id="224">
    <firstName>d</firstName>
    <lastName>d</lastName>
    <location>d</location>
  </employee>
</employees>
</db>

有什么办法可以解析员工的第一部分,即<employees name="aaa"></employees>

内的数据

我的java代码如下:

public static void main(String args[]) {
try {

File stocks = new File("C:\\employee.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(stocks);

doc.getDocumentElement().normalize();
deList nodes = doc.getElementsByTagName("employee");
for (int i = 0; i < nodes.getLength(); i++) {

Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("\nStock firstName: " + getValue("firstName", element));
}

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

private static String getValue(String tag, Element element) {

NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();

Node node = (Node) nodes.item(0);

return node.getNodeValue();

}

在此代码中,我无法标记为

deList nodes = doc.getElementsByTagName("employee name="aaa"");

我甚至创建了这个enitre名称的字符串并通过但仍然没有解决方案。 任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
//your code
XPath xPath =  XPathFactory.newInstance().newXPath();
String expression = "db/employees[@name='aaa']/employee";
NodeList nodeList =(NodeList)xPath.compile(expression).evaluate(xmlDocument,XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); 
}

答案 1 :(得分:0)

您可以使用XPath表达式。

public static void main(String[] args) {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    try {

        File stocks = new File("D:\\EmpDetails.xml");
        builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(stocks);
        XPath xPath = XPathFactory.newInstance().newXPath();
        String expression = "//employees[@name='aaa']/employee/firstName";
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
        //System.out.println("length is " + nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
        }

    } catch (SAXException ex) {
        Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex);
    } catch (XPathExpressionException ex) {
        Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}