如何在xml节点中获取属性

时间:2017-01-12 14:20:23

标签: javascript xml

我有一个更大的xml这个节点(对不起,希望你不要睡着):

<c:EntityMetadata xmlns:c="http://schemas.microsoft.com/xrm/2011/Metadata">
  <c:MetadataId>5358a6e8-96c9-de11-9a56-00137299e1c2</c:MetadataId>
  <c:HasChanged xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:ActivityTypeMask>0</c:ActivityTypeMask>
  <c:Attributes xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:AutoCreateAccessTeams xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:AutoRouteToOwnerQueue>false</c:AutoRouteToOwnerQueue>
  <c:CanBeInManyToMany>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canbeinmanytomany</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanBeInManyToMany>
  <c:CanBePrimaryEntityInRelationship>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canbeprimaryentityinrelationship</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanBePrimaryEntityInRelationship>
  <c:CanBeRelatedEntityInRelationship>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canberelatedentityinrelationship</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanBeRelatedEntityInRelationship>
  <c:CanChangeTrackingBeEnabled xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:CanCreateAttributes>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    cancreateattributes</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanCreateAttributes>
  <c:CanCreateCharts>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    cancreatecharts</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanCreateCharts>
  <c:CanCreateForms>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    cancreateforms</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanCreateForms>
  <c:CanCreateViews>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    cancreateviews</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanCreateViews>
  <c:CanEnableSyncToExternalSearchIndex>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canenablesynctoexternalsearchindex</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanEnableSyncToExternalSearchIndex>
  <c:CanModifyAdditionalSettings>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">true</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifyadditionalsettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanModifyAdditionalSettings>
  <c:CanTriggerWorkflow>false</c:CanTriggerWorkflow>
  <c:ChangeTrackingEnabled>true</c:ChangeTrackingEnabled>
  <c:Description>
    <a:LocalizedLabels xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
      <a:LocalizedLabel>
        <c:MetadataId xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
        <c:HasChanged xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
        <a:IsManaged>true</a:IsManaged>
        <a:Label>ממפה מנהלי אבטחה (משתמשים וצוותים) לזכויות גישה מאובטחת
        לשדות.</a:Label>
        <a:LanguageCode>1037</a:LanguageCode>
      </a:LocalizedLabel>
    </a:LocalizedLabels>
    <a:UserLocalizedLabel xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
      <c:MetadataId xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
      <c:HasChanged xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
      <a:IsManaged>true</a:IsManaged>
      <a:Label>ממפה מנהלי אבטחה (משתמשים וצוותים) לזכויות גישה מאובטחת
      לשדות.</a:Label>
      <a:LanguageCode>1037</a:LanguageCode>
    </a:UserLocalizedLabel>
  </c:Description>
  <c:DisplayCollectionName>
    <a:LocalizedLabels xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" />
    <a:UserLocalizedLabel xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  </c:DisplayCollectionName>
  <c:DisplayName>
    <a:LocalizedLabels xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" />
    <a:UserLocalizedLabel xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  </c:DisplayName>
  <c:EnforceStateTransitions>false</c:EnforceStateTransitions>
  <c:IconLargeName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:IconMediumName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:IconSmallName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:IsAIRUpdated>false</c:IsAIRUpdated>
  <c:IsActivity>false</c:IsActivity>
  <c:IsActivityParty>false</c:IsActivityParty>
  <c:IsAuditEnabled>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifyauditsettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsAuditEnabled>
  <c:IsAvailableOffline>true</c:IsAvailableOffline>
  <c:IsBusinessProcessEnabled>false</c:IsBusinessProcessEnabled>
  <c:IsChildEntity>false</c:IsChildEntity>
  <c:IsConnectionsEnabled>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifyconnectionsettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsConnectionsEnabled>
  <c:IsCustomEntity>false</c:IsCustomEntity>
  <c:IsCustomizable>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    iscustomizable</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsCustomizable>
  <c:IsDocumentManagementEnabled>false</c:IsDocumentManagementEnabled>
  <c:IsDocumentRecommendationsEnabled>false</c:IsDocumentRecommendationsEnabled>
  <c:IsDuplicateDetectionEnabled>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifyduplicatedetectionsettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsDuplicateDetectionEnabled>
  <c:IsEnabledForCharts>false</c:IsEnabledForCharts>
  <c:IsEnabledForTrace>false</c:IsEnabledForTrace>
  <c:IsImportable>false</c:IsImportable>
  <c:IsInteractionCentricEnabled>false</c:IsInteractionCentricEnabled>
  <c:IsIntersect>false</c:IsIntersect>
  <c:IsKnowledgeManagementEnabled>false</c:IsKnowledgeManagementEnabled>
  <c:IsMailMergeEnabled>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifymailmergesettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsMailMergeEnabled>
  <c:IsManaged>true</c:IsManaged>
  <c:IsMappable>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    ismappable</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsMappable>
  <c:IsOneNoteIntegrationEnabled>false</c:IsOneNoteIntegrationEnabled>
  <c:IsQuickCreateEnabled>false</c:IsQuickCreateEnabled>
  <c:IsReadOnlyInMobileClient>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifymobileclientreadonly</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsReadOnlyInMobileClient>
  <c:IsReadingPaneEnabled>true</c:IsReadingPaneEnabled>
  <c:IsRenameable>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    isrenameable</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsRenameable>
  <c:IsStateModelAware>false</c:IsStateModelAware>
  <c:IsValidForAdvancedFind>false</c:IsValidForAdvancedFind>
  <c:IsValidForQueue>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifyqueuesettings</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsValidForQueue>
  <c:IsVisibleInMobile>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifymobilevisibility</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsVisibleInMobile>
  <c:IsVisibleInMobileClient>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifymobileclientvisibility</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsVisibleInMobileClient>
  <c:LogicalName>principalattributeaccessmap</c:LogicalName>
  <c:ManyToManyRelationships xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:ManyToOneRelationships xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:ObjectTypeCode>43</c:ObjectTypeCode>
  <c:OneToManyRelationships xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:OwnershipType>None</c:OwnershipType>
  <c:PrimaryIdAttribute>principalattributeaccessmapid</c:PrimaryIdAttribute>
  <c:PrimaryNameAttribute xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:Privileges xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:RecurrenceBaseEntityLogicalName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:ReportViewName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:SchemaName>PrincipalAttributeAccessMap</c:SchemaName>
  <c:SyncToExternalSearchIndex>false</c:SyncToExternalSearchIndex>
  <c:IntroducedVersion>5.0.0.0</c:IntroducedVersion>
  <c:PrimaryImageAttribute xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:CanChangeHierarchicalRelationship>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canchangehierarchicalrelationship</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:CanChangeHierarchicalRelationship>
  <c:EntityHelpUrl xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:EntityHelpUrlEnabled>false</c:EntityHelpUrlEnabled>
  <c:CollectionSchemaName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:EntityColor xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:IsOptimisticConcurrencyEnabled>true</c:IsOptimisticConcurrencyEnabled>
  <c:Keys xmlns:d="http://schemas.microsoft.com/xrm/7.1/Metadata" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
  i:nil="true" />
  <c:LogicalCollectionName xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:DaysSinceRecordLastModified>9999</c:DaysSinceRecordLastModified>
  <c:EntitySetName>principalattributeaccessmaps</c:EntitySetName>
  <c:IsEnabledForExternalChannels>false</c:IsEnabledForExternalChannels>
  <c:IsOfflineInMobileClient>
    <a:CanBeChanged xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">true</a:CanBeChanged>
    <a:ManagedPropertyLogicalName xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">
    canmodifymobileclientoffline</a:ManagedPropertyLogicalName>
    <a:Value xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">false</a:Value>
  </c:IsOfflineInMobileClient>
  <c:IsPrivate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:IsSLAEnabled xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true" />
  <c:MobileOfflineFilters />
</c:EntityMetadata>

我需要c:SchemaName子节点。今天我这样做:

for (var i = 0 ; i < node.childNodes.length ; i++) {
    if (node.childNodes[i].tagName == "c:SchemaName") {
                var value = node.childNodes[i].innerHTML;         
            }
}

我是否有更好的方法可以在Javascript中实现这一点(当我有很多这样的节点时,它真的很慢)?

谢谢

1 个答案:

答案 0 :(得分:0)

我会在Xpath中使用build:

var schemaNameValue = 
     document.evaluate('//c:SchemaName/text()', node, null, XPathResult.ANY_TYPE, null);