2013-11-26 32 views
0

我已經讀取了字符串中的所有xml內容。現在我正在解析該字符串。 我解析IAS的代碼如下:xml.getelement標記名稱無法正常工作

parser=new DOMParser(); 
     xmlDoc=parser.parseFromString(output,"text/xml");//output is a string which contains xml 

     var region= xmlDoc.getElementsByTagName("Region"); 
     console.log("Region lenght"+region.length); 

有一個標籤名地區並有很多地區的在我的XML字符串。但我面臨的問題是這個xmlDoc.getelementBytagName只返回1個元素。

我的XML字符串的摘錄如下:

<?xml version="1.0"?> 
<!--OfflineHtm [version 1.1.0]--> 
<!--(C) Copyrights 2008-2015 ID Technologies Canada--> 
<Project> 
    <ProjectInformation> 
    <FileHeader>IDGIS_PROJECT</FileHeader> 
    <Details ProjectID="0" ProjectName="" Description="" StartDate="01/01/0001 00:00:00 AM" OwnerShip="" LastModified="01/01/0001 00:00:00 AM" Datum="WGS_84" Projection="DEFAULT" NoSignificantDecimals="8" ZoomCurrent="224" RasterHeight="0" Background="-1">Project Details</Details> 
    <ProjectBounds XMin="25.9527142400271" YMin="44.3228898448188" XMax="26.2404541400271" YMax="44.5703763448188" ExtentBounds="yes">Bounds</ProjectBounds> 
    </ProjectInformation> 
    <Layers> 
    <Layer ID="20" Description="" MinZoom="1" MaxZoom="1000000000" Visible="1" RemotHostType="LocalFile" RemotHost="" FolderName="Reference" Path="" LayerStatus="ReadWrite"> 
     <ParamList> 
     <DrawingParam LineColor="-4925225" FillColor="-16760448" SelectedLineColor="-3657166" LineType="0" LineWidth="1" IconType="0" Options="3" ZoomLimit="9E+99" LayerType="1" /> 
     </ParamList> 
     <TextParamList> 
     <TextParam FieldIndex="-1" FontName="Arial" Bold="0" Italic="0" StrikeOut="0" TextAngle="0" TextColor="-16777216" TextFontSize="12" TextPosition="1" Underline="0" MinZoom="1" MaxZoom="1000000000" ShowText="False" FontFector="1000" /> 
     </TextParamList> 
     <Regions> 
     <Region ID="0" FileName="Reference.rgn" FilePath="" FileType="IDGIS_ASCII" RemotHost="" RemotHostType="LocalFile" /> 
     </Regions>Reference</Layer> 
    <Layer ID="19" Description="" MinZoom="1" MaxZoom="1000000000" Visible="1" RemotHostType="LocalFile" RemotHost="" FolderName="Water Valve" Path="" LayerStatus="ReadWrite"> 
     <ParamList> 
     <DrawingParam LineColor="-4925225" FillColor="-16760448" SelectedLineColor="-3657166" LineType="0" LineWidth="1" IconType="0" Options="3" ZoomLimit="9E+99" LayerType="1" /> 
     </ParamList> 
     <TextParamList> 
     <TextParam FieldIndex="-1" FontName="Arial" Bold="0" Italic="0" StrikeOut="0" TextAngle="0" TextColor="-16777216" TextFontSize="12" TextPosition="1" Underline="0" MinZoom="1" MaxZoom="1000000000" ShowText="False" FontFector="1000" /> 
     </TextParamList> 
     <Regions> 
     <Region ID="0" FileName="Water Valve.rgn" FilePath="" FileType="IDGIS_ASCII" RemotHost="" RemotHostType="LocalFile" /> 
     </Regions>Water Valve</Layer> 
+0

你'Region'和'Regions'無論是在XML。你想要哪一個 ? – Ani

回答

1

JS

parser=new DOMParser(); 
    xmlDoc=parser.parseFromString(output,"text/xml");//output is a string which contains xml 

    var region= xmlDoc.getElementsByTagName("Regions"); // Regions 
    console.log("Region length"+region.length); 
+0

也許代碼註釋不足以說明解決方案。請給出簡短的故事,這是怎麼解決的。 – mico

+0

OP正在搜索「Region」而不是「Regions」。 '的getElementsByTagName( 「地區」)' – Ani