我的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的初学者..请帮助
答案 0 :(得分: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是结果数组.. 代码尚未测试..所以,如果这不起作用,请告诉我..
答案 1 :(得分: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