-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathXML_Helper.pbi
186 lines (154 loc) · 4.47 KB
/
XML_Helper.pbi
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
; +----------------+
; | XML_Helper.pbi |
; +----------------+
; | 2016.05.26 . Creation (PureBasic 5.42)
; | .06.01 . Added DeleteXMLChildren()
; | 2017.04.24 . Cleanup
CompilerIf (Not Defined(__XML_Helper_Included, #PB_Constant))
#__XML_Helper_Included = #True
CompilerIf (#PB_Compiler_IsMainFile)
EnableExplicit
CompilerEndIf
;-
;- Macros
Macro MainXMLNodeName(_XML)
GetXMLNodeNameSafe(MainXMLNode(_XML))
EndMacro
;-
;- Constants
#XML_Normal = #PB_XML_Normal
#XML_Comment = #PB_XML_Comment
#XML_Root = #PB_XML_Root
#XML_Default = #PB_Default
;-
;- Macros
Macro IsNormalXMLNode(_Node)
Bool(XMLNodeType(_Node) = #XML_Normal)
EndMacro
Macro RemoveXMLComments(_Node, _Recursive = #True)
DeleteXMLChildren((_Node), "", #XML_Comment, (_Recursive))
EndMacro
Macro RemoveXMLMetadata(_Node, _Recursive = #True)
DeleteXMLChildren((_Node), "", #PB_XML_CData, (_Recursive))
DeleteXMLChildren((_Node), "", #PB_XML_DTD, (_Recursive))
DeleteXMLChildren((_Node), "", #PB_XML_Instruction, (_Recursive))
EndMacro
;-
;- Procedures
Procedure.s GetXMLNodeNameSafe(*Node)
If (*Node And IsNormalXMLNode(*Node))
ProcedureReturn (GetXMLNodeName(*Node))
EndIf
ProcedureReturn ("")
EndProcedure
Procedure.i HasXMLAttribute(*Node, Attribute.s)
Protected Result.i = #False
If (*Node And IsNormalXMLNode(*Node) And Attribute)
If (ExamineXMLAttributes(*Node))
While (NextXMLAttribute(*Node))
If (XMLAttributeName(*Node) = Attribute)
Result = #True
Break
EndIf
Wend
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i ChildXMLNodeEx(*Node, n.i = 0, Name.s = "", Type.i = #XML_Default)
Protected *Result = #Null
If (*Node And IsNormalXMLNode(*Node) And (n >= 0))
If (Name And (Type = #XML_Default))
Type = #XML_Normal
EndIf
Protected i.i = 0
Protected ChildType.i
Protected *Child = ChildXMLNode(*Node)
While (*Child)
ChildType = XMLNodeType(*Child)
If ((Type = #XML_Default) Or (Type = ChildType))
If ((ChildType <> #XML_Normal) Or (Name = "") Or (GetXMLNodeName(*Child) = Name))
i + 1
If (i > n)
*Result = *Child
Break
EndIf
EndIf
EndIf
*Child = NextXMLNode(*Child)
Wend
EndIf
ProcedureReturn (*Result)
EndProcedure
Procedure.i NextXMLNodeEx(*Node, Name.s = "", Type.i = #XML_Default)
Protected *Result = #Null
If (*Node)
If (Name And (Type = #XML_Default))
Type = #XML_Normal
EndIf
Protected NodeType.i
*Node = NextXMLNode(*Node)
While (*Node)
NodeType = XMLNodeType(*Node)
If ((Type = #XML_Default) Or (Type = NodeType))
If ((NodeType <> #XML_Normal) Or (Name = "") Or (GetXMLNodeName(*Node) = Name))
*Result = *Node
Break
EndIf
EndIf
*Node = NextXMLNode(*Node)
Wend
EndIf
ProcedureReturn (*Result)
EndProcedure
Procedure.i XMLChildCountEx(*Node, Name.s = "", Type.i = #XML_Default, Recursive.i = #False)
Protected Result.i = 0
If (*Node And IsNormalXMLNode(*Node))
If (Name And (Type = #XML_Default))
Type = #XML_Normal
EndIf
Protected NodeType.i
Protected *Child = ChildXMLNode(*Node)
While (*Child)
NodeType = XMLNodeType(*Child)
If ((Type = #XML_Default) Or (Type = NodeType))
If ((NodeType <> #XML_Normal) Or (Name = "") Or (GetXMLNodeName(*Child) = Name))
Result + 1
EndIf
EndIf
If (Recursive And (NodeType = #XML_Normal))
Result + XMLChildCountEx(*Child, Name, Type, #True)
EndIf
*Child = NextXMLNode(*Child)
Wend
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i DeleteXMLChildren(*Node, Name.s = "", Type.i = #XML_Default, Recursive.i = #False)
Protected Result.i = 0
If (*Node And IsNormalXMLNode(*Node))
If (Name And (Type = #XML_Default))
Type = #XML_Normal
EndIf
Protected NodeType.i
Protected *Next
Protected *Child = ChildXMLNode(*Node)
While (*Child)
NodeType = XMLNodeType(*Child)
If (Recursive And (NodeType = #XML_Normal))
Result + DeleteXMLChildren(*Child, Name, Type, #True)
EndIf
*Next = NextXMLNode(*Child)
If ((Type = #XML_Default) Or (Type = NodeType))
If ((NodeType <> #XML_Normal) Or (Name = "") Or (GetXMLNodeName(*Child) = Name))
DeleteXMLNode(*Child)
Result + 1
EndIf
EndIf
*Child = *Next
Wend
EndIf
ProcedureReturn (Result)
EndProcedure
CompilerEndIf
;-