VB中如何把TXT转换成XML
--------------------编程问答-------------------- 一部分If objXML Is Nothing Then
Set objXML = New MSXML2.DOMDocument40
Set objBills = objXML.createElement(BillsTag)
objXML.appendChild objBills
SequenceNumber = 1
Else
SequenceNumber = SequenceNumber + 1
End If
Set objBill = objXML.createElement(BillTag)
objBills.appendChild objBill
Set objAttr = objXML.createAttribute(BillNumberTag)
objAttr.Text = FieldToString(objRSBillControl, 1)
objBill.Attributes.setNamedItem objAttr
' AddXMLAttribute BillNumberTag, FieldToString(objRSBillControl, 1)
AddXMLAttribute SequenceNumberTag, str(SequenceNumber)
AddXMLAttribute BillPeriodTag, Trim(cmbBillPeriod.Text)
AddXMLAttribute ContactCodeTag, FieldToString(objRSBillControl, 8)
AddXMLAttribute CompanyNameTag, FieldToString(objRSBillControl, 12)
AddXMLAttribute TitleTag, FieldToString(objRSBillControl, 2)
AddXMLAttribute FirstNameTag, FieldToString(objRSBillControl, 3)
AddXMLAttribute LastNameTag, FieldToString(objRSBillControl, 4)
AddXMLAttribute StateTag, FieldToString(objRSBillControl, 6) --------------------编程问答-------------------- 先研究下,请问可以给个完整的吗?
我是菜鸟 --------------------编程问答-------------------- 还有一点
objXML.LoadXML Replace(objXML.XML, "><", ">" & vbNewLine & "<")
' remove the new line for child node which does not have text
'when load using loadxml, it auot add the indent (fourspace) for children
'second level children has two fourspaces in front
objXML.LoadXML Replace(objXML.XML, ">" & vbNewLine & FourSpace & FourSpace & "</", "></")
objXML.Save ControlFilePathName
Set objXML = Nothing
--------------------编程问答-------------------- 请问还有吗?
能讲解下吗?万分感谢 --------------------编程问答-------------------- 请问有完整的代码吗? 急啊 求各位高手解答! --------------------编程问答-------------------- 这个问题已经解决了,但又有新问题了,生成的test.xml里有中文要用UTF-8显示,但VB默认生成的test.xml不是UTF-8,问题一:请问默认生成的格式是什么,问题二:有什么函数可以在open file的时候,就让test.xml变成UTF-8的,各位高手,帮忙看下吧!~!!!!!! --------------------编程问答-------------------- 学习帖,搜索了下,写了如下测试代码。
问题一:默认生成的格式一般就是UTF-8.
问题二:由于问题一,所以没必要。
Private Sub Command1_Click()
Dim iFileNo As Integer
Dim sData As String
Dim bData() As Byte
iFileNo = FreeFile()
Open AppPath & "data.txt" For Binary As #iFileNo
'如下这样读取,VB内部自动转换为Unicode
'sData = input(LOF(iFileNo), #iFileNo)
'如下这样读取,就是原汁原味的,这里默认就是UTF-8编码的
ReDim bData(LOF(iFileNo) - 1)
Seek #iFileNo, 1
Get #iFileNo, , bData
sData = StrConv(bData, vbUnicode)
Close #iFileNo
MsgBox sData
'Call Txt2Xml(sData, AppPath & "test.xml", "US-ASCII")
'Call Txt2Xml(sData, AppPath & "test.xml", "GB2312")
'Call Txt2Xml(sData, AppPath & "test.xml", "GBK")
'Call Txt2Xml(sData, AppPath & "test.xml", "UNICODE")
'Call Txt2Xml(sData, AppPath & "test.xml", "UTF-16")
Call Txt2Xml(sData, AppPath & "test.xml")
End Sub
Sub Txt2Xml(sText As String, sSavePathName As String, Optional encoding = "UTF-8")
Dim xmlDoc As New DOMDocument60
Dim objElemRoot As IXMLDOMElement
Dim objElemChild As IXMLDOMElement
Dim arrItems() As String, arrNameText() As String
Dim vData
With xmlDoc
Set objElemRoot = .createElement("INFO")
.appendChild objElemRoot
'
arrItems = Split(sText, ",")
For Each vData In arrItems
arrNameText = Split(vData, "=")
Set objElemChild = .createElement(Trim$(arrNameText(0)))
arrNameText(1) = Trim$(arrNameText(1))
objElemChild.Text = Mid$(arrNameText(1), 2, Len(arrNameText(1)) - 2)
'objElemChild.setAttribute "Attrib", "no"
'
objElemRoot.appendChild objElemChild
Next
'
.loadXML PrettyPrintXml(xmlDoc)
'http://us.generation-nt.com/answer/how-configure-msxml2-ixmldomdocument-emit-utf8-entities-instead-utf16-characters-indent-help-80225032.html
.insertBefore .createProcessingInstruction("xml", "version=""1.0"" encoding=""" & encoding & """"), .documentElement
.save sSavePathName
'
.Load sSavePathName
MsgBox .xml
End With
Erase arrNameText
Erase arrItems
Set objElemChild = Nothing
Set objElemRoot = Nothing
Set xmlDoc = Nothing
End Sub
'http://stackoverflow.com/questions/1144015/how-to-make-xmldomdocument-include-the-xml-declaration
Function PrettyPrintXml(xmlDoc As DOMDocument60) As String
Dim wrt As New MXXMLWriter60
Dim rdr As New SAXXMLReader60
' Set properties on the XML writer - including BOM, XML declaration and encoding
'wrt.byteOrderMark = True
'wrt.encoding ="UTF-8"
wrt.omitXMLDeclaration = True
wrt.indent = True
' Set the XML writer to the SAX content handler.
Set rdr.contentHandler = wrt
Set rdr.dtdHandler = wrt
Set rdr.errorHandler = wrt
rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt
' Now pass the DOM through the SAX handler, and it will call the writer
Call rdr.parse(xmlDoc)
PrettyPrintXml = wrt.output
Set rdr = Nothing
Set wrt = Nothing
补充:VB , 网络编程