Skip to content

Commit 6f31952

Browse files
committed
Merge pull request #4 from olofsen/master
add_cdata function
2 parents 6e035b5 + 313ac83 commit 6f31952

File tree

6 files changed

+32
-2
lines changed

6 files changed

+32
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ e = new_child(parent, name) # create a new element and add it as a child
263263
add_text(e, text) # add text content to an element
264264
# equiv. to new_textnode + add_child
265265

266+
add_cdata(xdoc, e, text) # add cdata content to an element
267+
# equiv. to new_cdatanode + add_child
268+
266269
set_attribute(e, name, value) # set an attribute of an element
267270
# this returns the added attribute
268271
# as an instance of XMLAttr

run_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests = ["parse", "create"]
1+
tests = ["parse", "create", "cdata"]
22

33
for t in tests
44
fpath = joinpath("test", "$t.jl")

src/LightXML.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module LightXML
1313
child_nodes, has_children, attributes, has_attributes, attributes_dict,
1414
child_elements, find_element, get_elements_by_tagname,
1515

16-
new_element, add_child, new_child, new_textnode, add_text,
16+
new_element, add_child, new_child, new_textnode, add_text, add_cdata,
1717
set_attribute, set_attributes,
1818

1919
# document
@@ -27,4 +27,5 @@ module LightXML
2727
include("utils.jl")
2828
include("nodes.jl")
2929
include("document.jl")
30+
include("cdata.jl")
3031
end

src/cdata.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function new_cdatanode(xdoc::XMLDocument, txt::ASCIIString)
2+
p = ccall(xmlNewCDataBlock, Xptr, (Xptr, Xstr, Cint), xdoc.ptr, txt, length(txt)+1)
3+
XMLNode(p)
4+
end
5+
6+
add_cdata(xdoc::XMLDocument, x::XMLElement, txt::ASCIIString) = add_child(x, new_cdatanode(xdoc,txt))

src/clib.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ _xcopystr(p::Xstr) = (r = bytestring(p); _xmlfree(p); r)
4949
@lx2func xmlNewNode
5050
@lx2func xmlAddChild
5151
@lx2func xmlNewText
52+
@lx2func xmlNewCDataBlock
5253
@lx2func xmlSetProp
5354

5455
# functions for documents

test/cdata.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using LightXML
2+
3+
xdoc = XMLDocument()
4+
5+
xroot = create_root(xdoc, "States")
6+
7+
xs1 = new_child(xroot, "State")
8+
add_cdata(xdoc, xs1, "Massachussetts")
9+
10+
rtxt = """
11+
<?xml version="1.0" encoding="utf-8"?>
12+
<States>
13+
<State><![CDATA[Massachussetts]]></State>
14+
</States>
15+
"""
16+
17+
@assert strip(string(xdoc)) == strip(rtxt)
18+
19+
free(xdoc)

0 commit comments

Comments
 (0)