0
 <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,而不仅仅是 USBankruptcy 下的那些。

有没有办法指定父母?

我试过 NodeList nl = doc.getElementsByTagName("USBankruptcies/CourtId");

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

4

2 回答 2

1

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

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

NodeList bankruptcyCourtNodes = bankruptcyElement.getElementsByTagName("CourtId");
// etc...
于 2012-04-11T17:53:54.853 回答
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));
    }
于 2012-04-11T17:41:58.720 回答