Splitting Xml Document according to node

后端 未结 2 1059
说谎
说谎 2021-01-26 06:39

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

 
  
    

        
相关标签:
2条回答
  • 2021-01-26 07:12

    Between <Placemark> and </Placemark> tag ..

    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 is result array .. The code not tested yet .. so, let me know if that's not working ..

    0 讨论(0)
  • 2021-01-26 07:26

    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
    
    0 讨论(0)
提交回复
热议问题