本文介绍了根据节点拆分 Xml 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My xml document looks like .. (its actually a kml file for google map..)

 <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 文件,例如

According to each Placemark for each folder node i will like to make a separate xml file like

第一个 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的初学者..请帮助

..i am very beginner in xml ..help please

推荐答案

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

Finally i made success for splitting xml according to node ..I have saved individual kml files according to node in xml ..Here is my solution

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

这篇关于根据节点拆分 Xml 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:01