从XML JAVA获取特定的子节点

时间:2019-01-30 14:24:08

标签: java xml filereader

<DistinctParty FixedRef="10569">
      <Comment />
      <Profile ID="10569" PartySubTypeID="4">
        <Identity ID="952" FixedRef="10569" Primary="true" False="false">
          <Alias FixedRef="10569" AliasTypeID="1403" Primary="true" LowQuality="false">
            <DocumentedName ID="952" FixedRef="10569" DocNameStatusID="1">
              <DocumentedNamePart>
                <NamePartValue NamePartGroupID="36916" ScriptID="215" ScriptStatusID="1" Acronym="false">MAKHLUF</NamePartValue>
              </DocumentedNamePart>
              <DocumentedNamePart>
                <NamePartValue NamePartGroupID="36917" ScriptID="215" ScriptStatusID="1" Acronym="false">Hafiz</NamePartValue>
              </DocumentedNamePart>
            </DocumentedName>
          </Alias>
          <Alias FixedRef="10569" AliasTypeID="1400" Primary="false" LowQuality="false">
            <DocumentedName ID="12197" FixedRef="10569" DocNameStatusID="2">
              <DocumentedNamePart>
                <NamePartValue NamePartGroupID="18042" ScriptID="215" ScriptStatusID="1" Acronym="false">MAKHLOUF</NamePartValue>
              </DocumentedNamePart>
              <DocumentedNamePart>
                <NamePartValue NamePartGroupID="18043" ScriptID="215" ScriptStatusID="1" Acronym="false">Hafez</NamePartValue>
              </DocumentedNamePart>
            </DocumentedName>
          </Alias>
          <NamePartGroups>
            <MasterNamePartGroup>
              <NamePartGroup ID="36916" NamePartTypeID="1520" />
            </MasterNamePartGroup>
            <MasterNamePartGroup>
              <NamePartGroup ID="36917" NamePartTypeID="1521" />
            </MasterNamePartGroup>
            <MasterNamePartGroup>
              <NamePartGroup ID="18042" NamePartTypeID="1520" />
            </MasterNamePartGroup>
            <MasterNamePartGroup>
              <NamePartGroup ID="18043" NamePartTypeID="1521" />
            </MasterNamePartGroup>
          </NamePartGroups>
        </Identity>
        <Feature ID="5887" FeatureTypeID="8">
          <FeatureVersion ID="570" ReliabilityID="1">
            <Comment />
            <DatePeriod CalendarTypeID="1" YearFixed="false" MonthFixed="false" DayFixed="false">
              <Start Approximate="true" YearFixed="false" MonthFixed="false" DayFixed="false">
                <From>
                  <Year>1975</Year>
                  <Month>1</Month>
                  <Day>1</Day>
                </From>
                <To>
                  <Year>1975</Year>
                  <Month>1</Month>
                  <Day>1</Day>
                </To>
              </Start>
              <End Approximate="true" YearFixed="false" MonthFixed="false" DayFixed="false">
                <From>
                  <Year>1975</Year>
                  <Month>12</Month>
                  <Day>31</Day>
                </From>
                <To>
                  <Year>1975</Year>
                  <Month>12</Month>
                  <Day>31</Day>
                </To>
              </End>
            </DatePeriod>
            <VersionDetail DetailTypeID="1430" />
          </FeatureVersion>
          <IdentityReference IdentityID="952" IdentityFeatureLinkTypeID="1" />
        </Feature>
        <Feature ID="5888" FeatureTypeID="9">
          <FeatureVersion ID="571" ReliabilityID="1">
            <Comment />
            <VersionDetail DetailTypeID="1432">Damascus, Syria</VersionDetail>
          </FeatureVersion>
          <IdentityReference IdentityID="952" IdentityFeatureLinkTypeID="1" />
        </Feature>
        <Feature ID="164625" FeatureTypeID="25">
          <FeatureVersion ID="214625" ReliabilityID="1">
            <Comment />
            <VersionLocation LocationID="14625" />
          </FeatureVersion>
          <IdentityReference IdentityID="952" IdentityFeatureLinkTypeID="1" />
        </Feature>
        <Feature ID="310569" FeatureTypeID="26">
          <FeatureVersion ID="410569" ReliabilityID="1">
            <Comment />
            <VersionDetail DetailTypeID="1432">Colonel</VersionDetail>
          </FeatureVersion>
          <IdentityReference IdentityID="952" IdentityFeatureLinkTypeID="1" />
        </Feature>
      </Profile>
    </DistinctParty>

这是XML,其根是DistinctParty,现在我需要通过JAVA从中提取两个标签。

  1. NamePartyValue
  2. VersionDetail

现在,我获得了root元素,并且可以使用这些方法提取NamePartyValue,但是我还需要提取在结果中甚至找不到的VersionDetail元素,请您指导我一个准确的方向。

public NodeList readXMLFileUpdated(String path) {
        File fXmlFile = new File(path);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;
        Document doc = null;
        NodeList nList = null;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            nList = doc.getElementsByTagName("DistinctParty");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nList;
    }

public List<Company> getCompanyNamesXML(NodeList nodes){
        List<Company> listOfCompanies = new ArrayList<>();
        for(int i = 0; i<nodes.getLength(); i++)
            listOfCompanies.add(new Company(nodes.item(i).getTextContent()));
        return listOfCompanies;
    }

上部方法中的此语句 nodes.item(i).getTextContent()向我返回NamePartyValue字段的值,并在尝试使用此 nodes.item( i).getChildNodes(),我看不到结果中的VersionDetail,知道我可能会缺少什么吗?

1 个答案:

答案 0 :(得分:0)

就像通过调用doc.getElementsByTagName()找到了“ DistincyParty”的NodeList一样,您可以通过执行相同的操作来查找VersionDetail或NamePartValue元素。