简体   繁体   English

从XML JAVA获取特定的子节点

[英]Getting a specific child node from XML JAVA

<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>

This is an XML and its root is DistinctParty, now I need to extract two tags from it via JAVA. 这是XML,其根是DistinctParty,现在我需要通过JAVA从中提取两个标签。

  1. NamePartyValue NamePartyValue
  2. VersionDetail VersionDetail

Now I get the root element and can extract NamePartyValue with these methods, but I also need to extract VersionDetail element which I can't even find in the result, could you please direct me in an accurate direction. 现在,我获得了根元素,并可以使用这些方法提取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;
    }

This statement nodes.item(i).getTextContent() in upper method returns me the value of NamePartyValue field and when trying to get children of this node with this nodes.item(i).getChildNodes() I can't see VersionDetail in the result, any idea what I might be missing? 上面方法中的这个声明nodes.item(i).getTextContent()向我返回了NamePartyValue字段的值,当尝试使用该节点获取此节点的子级时。item(i).getChildNodes()我在以下版本中看不到VersionDetail结果,知道我可能会缺少什么吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM