-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLBuilder2.bas
80 lines (68 loc) · 2.42 KB
/
XMLBuilder2.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=7.8
@EndOfDesignText@
Sub Class_Globals
Private mDoc As JavaObject
Private bc As ByteConverter 'ignore
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Dim factory As JavaObject
factory.InitializeStatic("javax.xml.parsers.DocumentBuilderFactory")
Dim dbFactory As JavaObject
dbFactory=factory.RunMethod("newInstance",Null)
Dim builder As JavaObject
builder=dbFactory.RunMethod("newDocumentBuilder",Null)
mDoc=builder.RunMethod("newDocument",Null)
End Sub
Public Sub getDoc As JavaObject
Return mDoc
End Sub
Public Sub e(name As String) As JavaObject
Return createElement(name)
End Sub
Public Sub t(text As String) As JavaObject
Return createTextNode(text)
End Sub
Public Sub a(name As String,value As String) As JavaObject
Return createAttribute(name,value)
End Sub
Public Sub createElement(name As String) As JavaObject
Return mDoc.RunMethod("createElement",Array(name))
End Sub
Public Sub createTextNode(text As String) As JavaObject
Return mDoc.RunMethod("createTextNode",Array(text))
End Sub
Public Sub appendChild(rootElement As JavaObject, node As Object)
rootElement.RunMethod("appendChild",Array(node))
End Sub
Public Sub createAttribute(name As String,value As String) As JavaObject
Dim attr As JavaObject=mDoc.RunMethod("createAttribute",Array(name))
attr.RunMethod("setValue",Array(value))
Return attr
End Sub
Public Sub setAttributeNode(attr As Object,element As JavaObject)
element.RunMethod("setAttributeNode",Array(attr))
End Sub
Public Sub asString As String
Dim transformerFactory As JavaObject
transformerFactory.InitializeStatic("javax.xml.transform.TransformerFactory")
transformerFactory=transformerFactory.RunMethod("newInstance",Null)
Dim transformer As JavaObject
transformer=transformerFactory.RunMethod("newTransformer",Null)
Dim source As JavaObject
source.InitializeNewInstance("javax.xml.transform.dom.DOMSource",Array(mDoc))
Dim out As OutputStream
out=File.OpenOutput(File.DirTemp,"temp.xml",False)
Dim result As JavaObject
result.InitializeNewInstance("javax.xml.transform.stream.StreamResult",Array(out))
transformer.RunMethod("transform",Array(source,result))
out.Close
If File.Exists(File.DirTemp,"temp.xml") Then
Return File.ReadString(File.DirTemp,"temp.xml")
End If
Return ""
End Sub