Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
213 lines (160 loc) · 4.5 KB

File metadata and controls

executable file
·
213 lines (160 loc) · 4.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env luajit
-- Requires "xmlua" module
local xmlua = require("xmlua")
local html = [[
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>World</p>
</body>
</html>
]]
-- Parses HTML
local document = xmlua.HTML.parse(html)
-- Serializes to HTML
print(document:to_html())
-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
-- <html>
-- <head>
-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-- <title>Hello</title>
-- </head>
-- <body>
-- <p>World</p>
-- </body>
-- </html>
local xml = [[
<root>
<sub>text1</sub>
<sub>text2</sub>
<sub>text3</sub>
</root>
]]
-- Parses XML
local document = xmlua.XML.parse(xml)
-- Serializes to XML
print(document:to_xml())
-- <?xml version="1.0" encoding="UTF-8"?>
-- <root>
-- <sub>text1</sub>
-- <sub>text2</sub>
-- <sub>text3</sub>
-- </root>
local xmlua = require("xmlua")
local invalid_xml = "<root>"
-- Parses invalid XML
local success, document = pcall(xmlua.XML.parse, invalid_xml)
if success then
print("Succeeded to parse XML")
else
-- If pcall returns not success, the second return value is error
-- message not document.
local message = document
print("Failed to parse XML: " .. message)
-- -> Failed to parse XML: Premature end of data in tag root line 1
end
local xml = [[
<root>
<sub>text1</sub>
<sub>text2</sub>
<sub>text3</sub>
</root>
]]
local document = xmlua.XML.parse(xml)
-- Searches all <sub> elements under the <root> element
local all_subs = document:search("/root/sub")
-- You can use "#" for getting the number of matched nodes
print(#all_subs) -- -> 3
-- You can access the N-th node by "[]".
print(all_subs[1]:to_xml()) -- -> <sub1>text1</sub1>
print(all_subs[2]:to_xml()) -- -> <sub2>text2</sub2>
print(all_subs[3]:to_xml()) -- -> <sub3>text3</sub3>
local xml = [[
<root>
<sub class="A"><subsub1/></sub>
<sub class="B"><subsub2/></sub>
<sub class="A"><subsub3/></sub>
</root>
]]
local document = xmlua.XML.parse(xml)
-- Searches all <sub class="A"> elements
local class_a_subs = document:search("//sub[@class='A']")
-- Searches all elements under <sub class="A"> elements
local subsubs_in_class_a = class_a_subs:search("*")
print(#subsubs_in_class_a) -- -> 2
-- It's /root/sub[@class="A"]/subsub1
print(subsubs_in_class_a[1]:to_xml())
-- <subsub1/>
-- It's /root/sub[@class="A"]/subsub3
print(subsubs_in_class_a[2]:to_xml())
-- <subsub3/>
local xml = [[
<root>
<sub>text1</sub>
<sub>text2</sub>
<sub>text3</sub>
</root>
]]
local document = xmlua.XML.parse(xml)
-- Searches the <root> element
local roots = document:search("/root")
local root = roots[1]
-- Searches all <sub> elements under the <root> element
local all_subs = root:search("sub")
-- You can use "#" for getting the number of matched nodes
print(#all_subs) -- -> 3
-- You can access the N-th node by "[]".
print(all_subs[1]:to_xml()) -- -> <sub1>text1</sub1>
print(all_subs[2]:to_xml()) -- -> <sub2>text2</sub2>
print(all_subs[3]:to_xml()) -- -> <sub3>text3</sub3>
local xml = "<root/>"
local document = xmlua.XML.parse(xml)
local invalid_xpath = "..."
local success, node_set = pcall(function()
return document:search(invalid_xpath)
end)
if success then
print("Succeeded to search by XPath")
else
-- If pcall returns not success, the second return value is error
-- message not node set.
local message = node_set
print("Failed to search by XPath: " .. message)
-- -> Failed to search by XPath: Invalid expression
end
local document = xmlua.XML.parse("<root class='A'/>")
-- Gets the root element: <root>
local root = document:root()
-- Gets the root element: <root>
local root = document:search("/root")[1]
-- Uses dot syntax to get attribute value
print(root.class)
-- -> A
-- Uses [] syntax to get attribute value
print(root["class"])
-- -> A
-- Uses get_attribute method to get attribute value
print(root:get_attribute("class"))
-- -> A
-- Returns nil for nonexistent attribute name
print(root.nonexistent)
-- -> nil
local xml = [[
<root xmlns:example="http://example.com/"
example:attribute="value-example"
attribute="value"
nonexistent-namespace:attribute="value-nonexistent-namespace"/>
]]
local document = xmlua.XML.parse(xml)
local root = document:root()
-- With namespace prefix
print(root["example:attribute"])
-- -> value-example
-- Without namespace prefix
print(root["attribute"])
-- -> value
-- With nonexistent namespace prefix
print(root["nonexistent-namespace:attribute"])
-- -> value-nonexistent-namespace
Morty Proxy This is a proxified and sanitized view of the page, visit original site.