如何解析具有相同名称但父母不同的两个节点?

时间:2012-04-11 17:27:32

标签: java xml xpath xmldocument

 <PublicRecords>
      <USBankruptcies>
         <USBanktruptcy>...<USBankruptcy>
         <CourtId>...</CourtId>
         <USBanktruptcy>...<USBankruptcy>
         <CourtId>...</CourtId>
      </USBankruptcies>             
      <USTaxLiens>
         <USTaxLien>...<USTaxLien>
         <CourtId>...</CourtId>
         <USTaxLien>...<USTaxLien>
         <CourtId>...</CourtId>
      </USTaxLiens>       
      <USLegalItems>
         <USLegalItem><USLegalItem>
         <CourtId></CourtId>
          <USLegalItem><USLegalItem>
         <CourtId></CourtId>
      </USLegalItems>       
  </PubicRecords>

我正在使用doc和xpath对象的组合来提取属性和节点内容。

    NodeList bp = doc.getElementsByTagName("USBankruptcy");
    NodeList nl = doc.getElementsByTagName("CourtId");
    long itrBP;
    for (itrBP = 0; itrBP < bp.getLength(); itrBP++ )
    {

        Element docElement = (Element) bp.item(itrBP);
        Element courtElement = (Element) nl.item(itrBP);



        NodeList df = docElement.getElementsByTagName("DateFiled");
        if(df.getLength() > 0)
        {
            dateFiled = nullIfBlank(((Element)df.item(0)).getFirstChild().getTextContent());
            dateFiled = df.format(dateFiled);
        }

但是,当我说获取标签名称CourtID的元素时,它将获得所有CourtID,而不仅仅是USB破产下的那些。

有没有办法指定父母?

我尝试了NodeList nl = doc.getElementsByTagName(“USBankruptcies / CourtId”);

它在运行时给了我一个dom错误。

2 个答案:

答案 0 :(得分:1)

请在此处找到代码:

DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("test.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("*//USBankruptcies/CourtId");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i));
    }

答案 1 :(得分:1)

不是在Document上调用getElementsByTagName("CourtId")方法,而是在子元素(在您的情况下为<USBankruptcies>元素)上调用它。

NodeList bankruptcyNodes = doc.getElementsByTagName("USBankruptcies");
Element bankruptcyElement = (Element) bankruptcyNodes.item(0);

NodeList bankruptcyCourtNodes = bankruptcyElement.getElementsByTagName("CourtId");
// etc...