0

我的 xml 文档看起来像..(它实际上是一个用于谷歌地图的 kml 文件..)

 <kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Folder>
      <Name>Folder1</Name>
      <Placemark>
        <Name>Placemark1Folder1</Name>
        <LookAt>
          <longitude>-122.0839597145766</longitude>
          <latitude>37.42222904525232</latitude>
        </LookAt>
      </Placemark>
      <Placemark>
        <Name>Placemark2Folder1</Name>
        <LookAt>
          <longitude>-101.083959</longitude>
          <latitude>26.422</latitude>
        </LookAt>
      </Placemark>
    </Folder>
    <Folder>
      <Name>Folder2</Name>
      <Placemark>
        <Name>Placemark1Folder2</Name>
        <LookAt>
          <longitude>-96.566556</longitude>
          <latitude>14.422</latitude>
        </LookAt>
      </Placemark>
    </Folder>
  </Document>
</kml>

根据每个文件夹节点的每个地标,我想制作一个单独的 xml 文件,例如

第一个 XML:

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Folder>
      <Name>Folder1</Name>
      <Placemark>
        <Name>Placemark1Folder1</Name>
        <LookAt>
          <longitude>-122.0839597145766</longitude>
          <latitude>37.42222904525232</latitude>
        </LookAt>
      </Placemark>
    </Folder>
  </Document>
</kml>

第二个xml

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Folder>
      <Name>Folder1</Name>
      <Placemark>
        <Name>Placemark2Folder1</Name>
        <LookAt>
          <longitude>-101.083959</longitude>
          <latitude>26.422</latitude>
        </LookAt>
      </Placemark>
    </Folder>
  </Document>
</kml>

第三个xml

<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Folder>
      <Name>Folder2</Name>
      <Placemark>
        <Name>Placemark1Folder2</Name>
        <LookAt>
          <longitude>-96.566556</longitude>
          <latitude>14.422</latitude>
        </LookAt>
      </Placemark>
    </Folder>
  </Document>
</kml>

..我是 xml 的初学者 ..请帮忙

4

2 回答 2

1

之间<Placemark></Placemark>标记..

dim strXML as string = .... 'place your XML to be splitted here
dim x as integer     
Dim aXML As New List(Of String)
dim sAdd1 as String = '<kml xmlns="http://www.opengis.net/kml/2.2"><Document><Folder> <Name>Folder1</Name><Placemark>'

dim sAdd2 as String = '</Placemark></Folder></Document></kml>'

while true    
x=instr(strXML,"<Placemark>")
if x > 0 then
  strXML = mid(strXML,x+11) 
  x=instr(strXML,"</Placemark>")

  aXML.Add(sAdd1 & mid(strXML,1,x-1) & sAdd2)

  strXML = mid(strXML,x+12)
  strXML = trim(strXML)
  if strXML.length=0 then exit while
else
  exit while
endif    
loop

aXML 是结果数组 .. 代码尚未测试 .. 所以,如果这不起作用,请告诉我 ..

于 2013-05-20T10:09:52.257 回答
0

最后,我根据节点成功拆分 xml ..我已经根据 xml 中的节点保存了单个 kml 文件 ..这是我的解决方案

Public Sub SplitXml(ByVal XmlDoc As XmlDocument, ByVal SaveLocation As String)

        Dim TmpXml As XmlDocument = XmlDoc
        Dim Str As String = "<?xml version=""1.0"" encoding=""UTF-8""?>" & "<kml xmlns=" & Chr(34) & "http://www.opengis.net/kml/2.2" & Chr(34) & ">" & "<Document>"
        Dim DocumentNodes As XmlNodeList = TmpXml.GetElementsByTagName("Document")
        '=======================
        'Building Common String 
        '=======================
        For Each node As XmlNode In DocumentNodes
            Dim DocumentChildNodes As XmlNodeList = node.ChildNodes
            For Each Childnode As XmlNode In DocumentChildNodes
                If Childnode.Name <> "Folder" Then
                    Str = Str & Childnode.OuterXml.Replace("xmlns=""http://www.opengis.net/kml/2.2""", "")
                Else
                    Exit For
                End If
            Next
        Next

        Dim FolderNodes As XmlNodeList = TmpXml.GetElementsByTagName("Folder")
        Dim FolderName As String = String.Empty
        Dim XmlDocSave As XmlDocument = New XmlDocument()
        Dim StrXml As String = String.Empty
        Dim TmpStr As String = String.Empty
        Dim FileName As String = String.Empty
        For Each node As XmlNode In FolderNodes
            '==============================================================
            'Creating Directories For kml Getting Name from FirstChild Node
            '===============================================================
            FolderName = DirectCast(DirectCast(node, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
            FolderName = FolderName.Replace(".", "_")
            FolderName = FolderName.Replace(" ", "")
            If (Not System.IO.Directory.Exists(SaveLocation & "\" & FolderName)) Then
                System.IO.Directory.CreateDirectory(SaveLocation & "\" & FolderName)
            End If
            '==============================================================
            'Creating kml Files Getting Name from FirstChild Node
            '===============================================================
            Dim FolderChildNodes As XmlNodeList = node.ChildNodes
            For Each childnode As XmlNode In FolderChildNodes
                If childnode.Name = "Placemark" Then
                    FileName = DirectCast(DirectCast(childnode, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
                    FileName = FileName.Replace(".", "_")
                    FileName = FileName.Replace(" ", "")
                    StrXml = Str & "<Folder>" & TmpStr & childnode.OuterXml & "</Folder>" & "</Document>" & "</kml>"

                    XmlDocSave.LoadXml(StrXml)
                    XmlDocSave.Save(SaveLocation & "\" & FolderName & "\" & FileName & ".kml")
                    XmlDocSave = New XmlDocument()
                    StrXml = String.Empty
                Else
                    TmpStr = TmpStr & childnode.OuterXml
                End If
            Next
            TmpStr = String.Empty
        Next
    End Sub
于 2013-05-21T10:38:51.343 回答