获取xml中所有id属性的值,并查看是否有任何重复项

时间:2011-09-06 16:45:31

标签: java xml

如何测试XML文档以确定任何id属性是否具有重复值?

示例JSFF:

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
          xmlns:c="http://java.sun.com/jsp/jstl/core"
          xmlns:f="http://java.sun.com/jsf/core">
<c:set var="bundle" value="#{adfBundle['oracle.communications.inventory.ui.common.bundle.InventoryUIOverrideBundle']}"/>          
  <af:pageTemplate viewId="/oracle/communications/platform/templates/fragments/CommsUIShellMainArea.jspx" id="pt1">
    <f:facet name="localArea">
        <af:panelStretchLayout id="pgl1" binding="#{backingBeanScope.BI_summary_panelGroupLayout}" topHeight="20">
          <f:facet name="top">
          <af:panelHeader text="#{inventoryUIBundle.BUSINESS_INTERACTION_SUMMARY} - #{pageFlowScope.BusinessInteractionSummaryBean.displayInfo}"
                          id="PAGE_TITLE" styleClass="invSummaryPanelHeader" helpTopicId="OHW_busIntSummary">
            <f:facet name="context"/>
            <f:facet name="menuBar">
              <af:group rendered="#{!pageFlowScope.BusinessInteractionSummaryBean.previewLayout}">
                <af:menuBar id="menuBar2">
                  <af:menu id="menu6" binding="#{pageFlowScope.BusinessInteractionSummaryBean.actionsMenu}" styleClass="invSummaryMenu" 
                           disabled="#{pageFlowScope.BusinessInteractionSummaryBean.actionsMenuDisabled}"/>
                  <af:separator id="separator1" styleClass="invSummarySeparator"/>
                </af:menuBar>
              </af:group>
            </f:facet>
          </af:panelHeader>
          </f:facet>
          <f:facet name="center">
          <af:panelGroupLayout layout="scroll" id="pgl23">
          <af:panelHeader text="" id="ld12">
            <af:showDetailHeader text="#{inventoryUIBundle.BUSINESS_INTERACTION_INFO}" disclosed="true" id="sdh2"
                                 styleClass="invSummaryShowDetailHeader">
              <f:facet name="legend"/>
              <f:facet name="info"/>
              <f:facet name="menuBar"/>
              <f:facet name="toolbar">
                <af:toolbar id="t1">
                  <af:commandButton text="#{inventoryUIBundle.EDIT}" action="edit" id="cb1666" actionListener="#{bindings.openSubTaskForEdit.execute}"
                        rendered="#{!pageFlowScope.BusinessInteractionSummaryBean.previewLayout}"
                        disabled="#{pageFlowScope.BusinessInteractionSummaryBean.editButtonDisabled or !securityContext.taskflowViewable['/WEB-INF/oracle/communications/inventory/ui/businessinteraction/flow/BusinessInteractionEditFlow.xml#BusinessInteractionEditFlow']}">
                        <af:setActionListener from="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.oid}" to="#{objectId}"/>
                        <af:setActionListener from="false" to="#{addMode}"/>
                  </af:commandButton>
                </af:toolbar>
              </f:facet>
              <f:facet name="context"/>
              <af:panelFormLayout id="bi" maxColumns="2" rows="2" labelWidth="33%" fieldWidth="67%">
                <af:panelLabelAndMessage label="#{inventoryUIBundle.ID}"
                                         id="bi-id">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.id}" id="ot4"/>
                </af:panelLabelAndMessage>
                <af:panelLabelAndMessage label="#{inventoryUIBundle.NAME}"
                                         id="bi-name">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.name}" id="ot3"/>
                </af:panelLabelAndMessage>
                <af:panelLabelAndMessage label="#{inventoryUIBundle.SPECIFICATION}"
                                         id="bi-specification">
                    <af:commandLink action="SpecificationSummary" id="clSpec" actionListener="#{bindings.openSubTaskForSpec.execute}">
                        <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.specification.name}" id="ot10"/>
                        <af:setActionListener from="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.specification.oid}" to="#{specObjectId}"/>
                    </af:commandLink>
                </af:panelLabelAndMessage>
                <af:panelLabelAndMessage label="#{inventoryUIBundle.EFFECTIVE_DATE}"
                                         id="bi-effdate">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.effDate}"
                                 id="ot1">
                    <af:convertDateTime pattern="#{pageFlowScope.BusinessInteractionSummaryBean.dateTimeFormat}"/>
                  </af:outputText>
                </af:panelLabelAndMessage>
                <af:panelLabelAndMessage label="#{inventoryUIBundle.STATUS}"
                                         id="bi-adminstate">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.inventoryStatus}"
                                 id="ot6"/>
                </af:panelLabelAndMessage>
                <af:panelLabelAndMessage label="#{inventoryUIBundle.DESCRIPTION}"
                                         id="bi-description">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.description}"
                                 id="ot2"/>
                </af:panelLabelAndMessage>
            <af:panelLabelAndMessage label="#{inventoryUIBundle.LAST_MODIFIED_DATE}" id="bi-lmd" rendered="false">
                <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.lastModifiedDate}" id="ot9"/>
            </af:panelLabelAndMessage>
            <af:panelLabelAndMessage label="#{inventoryUIBundle.LAST_MODIFIED_USER}" id="bi-lmu" rendered="false">
                <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.lastModifiedUser}"
                               id="ot12"/>
            </af:panelLabelAndMessage>

              </af:panelFormLayout>

              <!-- show detail collapsible section for external identity -->
              <af:showDetail disclosed="#{pageFlowScope.BusinessInteractionSummaryBean.externalIdentityPopulated}" id="sd1"
                             disclosedText="#{inventoryUIBundle.EXTERNAL_SYSTEM}" undisclosedText="#{inventoryUIBundle.EXTERNAL_SYSTEM}"
                             inlineStyle="width:50%;">
                <af:panelFormLayout id="pfl101" maxColumns="1" rows="2" fieldWidth="69%" labelWidth="31%">
                    <af:panelLabelAndMessage label="#{inventoryUIBundle.EXTERNAL_MANAGEMENT_DOMAIN}" id="plam101">
                        <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.externalManagementDomain}" id="ot101"/>
                    </af:panelLabelAndMessage>
                    <af:panelLabelAndMessage label="#{inventoryUIBundle.EXTERNAL_NAME}" id="plam102">
                        <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.externalName}" id="ot102"/>
                    </af:panelLabelAndMessage>
                    <af:panelLabelAndMessage label="#{inventoryUIBundle.EXTERNAL_OBJECT_ID}" id="plam103">
                        <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.entityObject.externalObjectId}" id="ot103"/>
                    </af:panelLabelAndMessage>
                </af:panelFormLayout>
              </af:showDetail>

            </af:showDetailHeader>
            <af:group rendered="#{!pageFlowScope.BusinessInteractionSummaryBean.previewLayout}" id ="group2" >
            <af:spacer width="10" height="10" id="sp1"/>
            <!-- BI Hierarchy -->

            <af:showDetailHeader text="#{inventoryUIBundle.BUSINESS_INTERACTION_HIERARCHY}"
                               disclosed="true" id="sdh5">
            <f:facet name="context"/>
            <f:facet name="menuBar"/>
            <f:facet name="toolbar"/>
            <f:facet name="legend"/>
            <f:facet name="info"/>            
            <af:panelCollection id="pc2" styleClass="invSummaryPanelCollection" featuresOff="freeze wrap">
              <f:facet name="menus"/>
              <f:facet name="toolbar"/>
              <f:facet name="statusbar"/>
              <af:treeTable id="biTreeTable"
                            value="#{pageFlowScope.BusinessInteractionTreeBean.treeModel}"
                            binding="#{backingBeanScope.BI_treeBinding}"
                            disclosedRowKeys="#{pageFlowScope.BusinessInteractionTreeBean.treeDisclosedRowKeys}"
                            var="node" rowSelection="single" styleClass="invSummaryTree"
                            fetchSize="#{pageFlowScope.BusinessInteractionSummaryBean.fetchSize}"
                            contextMenuSelect="true" contentDelivery="immediate"
                            selectionListener="#{pageFlowScope.BusinessInteractionTreeBean.nodeSelectedEvent}"
                            rowBandingInterval="0" expandAllEnabled="#{pageFlowScope.BusinessInteractionTreeBean.expandAllEnabled}"
                            horizontalGridVisible="false">
                <f:facet name="nodeStamp">
                  <af:column  id="c9" width="500">
                    <af:panelGroupLayout id="pgl2">
                       <af:image source="#{node.data.icon}" id="nodeIcon" styleClass="invSummaryImage" shortDesc="icon"/>
                         <af:switcher id="idSwitcher" defaultFacet="false" facetName="#{pageFlowScope.BusinessInteractionTreeBean.treeModel.depth le 0}">
                            <f:facet name="false">           
                                <af:commandLink action="#{pageFlowScope.BusinessInteractionTreeBean.view}" id="cl1" actionListener="#{pageFlowScope.BusinessInteractionTreeBean.registerRecentItem}">
                                <af:setActionListener from="#{pageFlowScope.BusinessInteractionTreeBean.selectedTreeNode.object.oid}"
                                                  to="#{pageFlowScope.objectId}"/>
                                    <af:outputText value="#{node.data.label}" id="outputText22"/>
                                </af:commandLink>
                            </f:facet> 
                            <f:facet name="true">           
                                <af:outputText value="#{node.data.label}" id="ot8"/>
                            </f:facet> 
                         </af:switcher>
                     </af:panelGroupLayout>
                  </af:column>
                </f:facet>

                <f:facet name="contextMenu">
                  <af:popup contentDelivery="lazyUncached" id="popupContext"
                            partialTriggers="::biTreeTable">
                    <af:menu id="rootNodeMenu" text="rootNodeMenu">
                      <af:commandMenuItem text="#{inventoryUIBundle.ADD_CHILD}" id="treeMenuItem1" selected="true"
                                          rendered="#{(pageFlowScope.BusinessInteractionTreeBean.rootNodePopupMenu || pageFlowScope.BusinessInteractionTreeBean.childNodePopupMenu) and securityContext.userGrantedResource['resourceType=PAGE_ACTION,resourceName=BusinessInteraction.ASSOCIATE_CHILD_BI;action=view']}">
                        <af:showPopupBehavior popupId="::childBIPopup"/>
                      </af:commandMenuItem>

                       <af:commandMenuItem text="#{inventoryUIBundle.REMOVE}" id="treeMenuItem2"
                                           actionListener="#{pageFlowScope.BusinessInteractionTreeBean.removeFromBusinessInteraction}"
                                           rendered="#{pageFlowScope.BusinessInteractionTreeBean.childNodePopupMenu and securityContext.userGrantedResource['resourceType=PAGE_ACTION;resourceName=BusinessInteraction.DISASSOCIATE_CHILD_BI;action=view']}">
                        <af:clientListener method="goRefreshEntity"
                                           type="click"/>
                        <af:serverListener type="refreshEntity"
                                           method="#{pageFlowScope.BusinessInteractionTreeBean.refreshTree}"/>
                      </af:commandMenuItem>
                    </af:menu>
                  </af:popup>
                </f:facet>
            </af:treeTable>

              <af:popup id="childBIPopup" binding="#{backingBeanScope.popupChildBIBinding}" contentDelivery="lazyUncached">
                <af:dialog title="#{inventoryUIBundle.BUSINESS_INTERACTION_ASSOCIATE_CHILDREN}" id="d1" type="none">
                <af:panelStretchLayout id="psl_1" styleClass="invCommonPanelStretchLayout">
                  <f:facet name="center">                
                    <af:region value="#{bindings.BusinessInteractionSearchResultsFlow1.regionModel}"
                               id="r2" styleClass="invSummaryRegion"/>
                  </f:facet>
                </af:panelStretchLayout>
                  </af:dialog>
                <af:clientListener method="goRefreshEntity" type="popupClosed"/>
                <af:serverListener type="refreshEntity" method="#{pageFlowScope.BusinessInteractionTreeBean.refreshTree}"/>
              </af:popup>
            </af:panelCollection>
          </af:showDetailHeader>

            <!--Parent BI-->
            <af:spacer width="10" height="10" id="sp2"/>
            <af:showDetailHeader text="#{inventoryUIBundle.PARENT_BUSINESS_INTERACTIONS}"
                                 disclosed="true" id="sdh3"
                                 styleClass="invSummaryShowDetailHeader">
              <f:facet name="legend"/>
              <f:facet name="info"/>
              <f:facet name="menuBar"/>
              <f:facet name="toolbar"/>
              <f:facet name="context"/>
              <af:panelCollection id="pc1" styleClass="invSummaryPanelCollection">
              <f:facet name="menus"/>
              <f:facet name="toolbar"/>
              <f:facet name="statusbar">
                  <af:toolbar id="tb2">
                         <af:outputText value="#{inventoryUIBundle.ROW_COUNT} #{(pageFlowScope.BusinessInteractionSummaryBean.parentBusinessInteractionCollectonModel.rowCount lt 0) ? 0 :  pageFlowScope.BusinessInteractionSummaryBean.parentBusinessInteractionCollectonModel.rowCount}"
                                           id="ol1" styleClass="invSearchOutputLabel" partialTriggers="t2" binding="#{backingBeanScope.totalTPResultsLabel}"/>
                    </af:toolbar>
              </f:facet>
              <af:table value="#{pageFlowScope.BusinessInteractionSummaryBean.parentBusinessInteractionCollectonModel}"
                        var="row"
                        rows="#{bindings.BusinessInteraction.rangeSize}"
                        emptyText="#{inventoryUIBundle.TABLE_EMPTY_TEXT_NO_ROWS_YET}"
                        fetchSize="#{bindings.BusinessInteraction.rangeSize}"
                        autoHeightRows="#{pageFlowScope.BusinessInteractionSummaryBean.portletHeight}" contentDelivery="immediate"
                        rowBandingInterval="0" styleClass="invSummaryTable"
                        selectedRowKeys="#{bindings.BusinessInteraction.collectionModel.selectedRow}"
                        selectionListener="#{pageFlowScope.BusinessInteractionSummaryBean.parentBusinessInteractionSelectionListener}"
                        rowSelection="single" id="t2">
                 <af:column sortProperty="id" sortable="true"
                   headerText="#{inventoryUIBundle.ID}"
                   id="c1">
                   <af:commandLink action="init" id = "cl2" actionListener="#{bindings.openSubTaskForParentBusinessInteraction.execute}">
                        <af:outputText value="#{row.id}" id="ot9"/>
                        <af:setActionListener from="#{row.oid}" to="#{pageFlowScope.objectId}"/>
                    </af:commandLink>
                </af:column>

                <af:column sortProperty="name" sortable="true"
                           headerText="#{inventoryUIBundle.NAME}" id="c4">
                  <af:outputText value="#{row.name}" id="ot5"/>
                </af:column>
                <af:column sortProperty="specification.name" sortable="true"
                           headerText="#{inventoryUIBundle.SPECIFICATION}"
                           id="c3">
                  <af:commandLink action="SpecificationSummary" id="clSpec_ot7">
                        <af:outputText value="#{row.specification.name}" id="ot7"/>
                        <af:setActionListener from="#{row.specification.oid}" to="#{specObjectId}"/>
                  </af:commandLink>
                </af:column>
                <af:column sortProperty="adminState" sortable="true"
                           headerText="#{inventoryUIBundle.STATUS}" id="c8">
                  <af:outputText value="#{pageFlowScope.BusinessInteractionSummaryBean.parentStatus}"
                                 id="ot81"/>
                </af:column>
                <af:column sortProperty="effDate" sortable="true"
                           headerText="#{inventoryUIBundle.EFFECTIVE_DATE}"
                           id="c2">
                  <af:outputText value="#{row.effDate}" id="ot11">
                    <af:convertDateTime pattern="#{pageFlowScope.BusinessInteractionResultsBean.dateTimeFormat}"/>
                  </af:outputText>
                </af:column>
              </af:table>
              </af:panelCollection>
            </af:showDetailHeader>
            <af:spacer width="10" height="10" id="sp3"/>
            <af:region value="#{bindings.BusinessInteractionItemListFlow1.regionModel}" id="r1"/>
            <af:spacer width="10" height="10" id="bs3"/>
            <af:showDetailHeader text="#{inventoryUIBundle.ENTITY_ATTACHMENTS}" disclosed="true" id="sdh32" styleClass="invSummaryShowDetailHeader">
              <f:facet name="legend"/>
              <f:facet name="info"/>
              <f:facet name="menuBar"/>
              <f:facet name="toolbar"/>
              <f:facet name="context"/>
              <af:panelCollection id="pc16" styleClass="invSummaryPanelCollection">
              <f:facet name="menus"/>
              <f:facet name="toolbar"/>
              <f:facet name="statusbar">
                  <af:toolbar id="tb23">
                         <af:outputText value="#{inventoryUIBundle.ROW_COUNT} #{(pageFlowScope.BusinessInteractionSummaryBean.attachmentCollectionModel.rowCount lt 0) ? 0 :  pageFlowScope.BusinessInteractionSummaryBean.attachmentCollectionModel.rowCount}"
                                        id="ol134" styleClass="invSearchOutputLabel" noWrap="true" partialTriggers="attTab" binding="#{backingBeanScope.totalResultsLabel}"/>
                  </af:toolbar>
              </f:facet>
              <af:table value="#{pageFlowScope.BusinessInteractionSummaryBean.attachmentCollectionModel}" var="row"
                        emptyText="#{inventoryUIBundle.TABLE_EMPTY_TEXT_NO_ROWS_YET}"
                        binding="#{pageFlowScope.BusinessInteractionSummaryBean.attachmentTable}"
                        autoHeightRows="#{pageFlowScope.BusinessInteractionSummaryBean.portletHeight}" contentDelivery="immediate"
                        rowBandingInterval="0" styleClass="invSummaryTable"
                        rowSelection="single" id="attTab">
                   <af:column headerText="#{inventoryUIBundle.SEQUENCE_NUMBER}" id="x1">
                       <af:commandLink id="xl1" action="BusinessInteractionAttachmentSummary" actionListener="#{bindings.openSubTaskForBIAttachment.execute}"> 
                          <af:outputText value="#{row.sequenceNumber}" id="xot9"/>
                          <af:setActionListener from="#{row.biAttachmentObjectID}" to="#{pageFlowScope.objectId}"/>
                       </af:commandLink>
                   </af:column>
                   <af:column headerText="#{inventoryUIBundle.NAME}" id="x3">
                       <af:commandLink id="xl2" action="BusinessInteractionAttachmentSummary" actionListener="#{bindings.openSubTaskForBIAttachment.execute}"> 
                          <af:outputText value="#{row.name}" id="oxt7"/>
                          <af:setActionListener from="#{row.biAttachmentObjectID}" to="#{pageFlowScope.objectId}"/>
                       </af:commandLink>
                   </af:column>
                   <af:column headerText="#{inventoryUIBundle.TIMESTAMP}" id="x4">
                       <af:outputText value="#{row.timestamp}" id="xot5"/>
                   </af:column>
                 </af:table>
              </af:panelCollection>
            </af:showDetailHeader>
            </af:group>
          </af:panelHeader>
        </af:panelGroupLayout>
        </f:facet>
        </af:panelStretchLayout>
    </f:facet>
    <f:facet name="contextualArea"/>
  </af:pageTemplate>
</jsp:root>

1 个答案:

答案 0 :(得分:1)

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = factory.newDocumentBuilder().parse(Dupes.class.getResourceAsStream("jsff.xml"));
XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//@id");
Set<String> ids = new HashSet<String>();
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
int dupes = 0;
for (int i = 0; i < nodes.getLength(); i++) {
    String id = nodes.item(i).getNodeValue();
    if (ids.contains(id)) {
         System.out.format("%s is duplicate\n\n",id);
         dupes++;
    } else {
        ids.add(id);
    }
}
System.out.format("Total ids = %d\nTotal Duplicates = %d", ids.size(), dupes);

输出:

ot9 is duplicate

Total ids = 90
Total Duplicates = 1