Skip to content

Commit 1b55588

Browse files
committed
Merge pull request #27 from JuliaLang/tk/readfile
add xmlReadFile and parse_file with options
2 parents da4544c + 5f32642 commit 1b55588

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ set_attributes(e, key1="val1", key2="val2", ...)
292292

293293
```julia
294294
xdoc = parse_file(filename) # parse an XML file
295+
xdoc = parse_file(filename, # parse an XML file with a specified encoding and parser options,
296+
encoding, options) # see http://xmlsoft.org/html/libxml-parser.html#xmlReadFile
297+
# and http://xmlsoft.org/html/libxml-parser.html#xmlParserOption
295298
xdoc = parse_string(str) # parse an XML doc from a string
296299
save_file(xdoc, filename) # save xdoc to an XML file
297300

src/document.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ function parse_file(filename::AbstractString)
9090
XMLDocument(p)
9191
end
9292

93+
function parse_file(filename::AbstractString, encoding, options::Integer)
94+
p = ccall((:xmlReadFile,libxml2), Xptr, (Cstring, Ptr{Cchar}, Cint),
95+
filename, encoding, options)
96+
p != C_NULL || throw(XMLParseError("Failure in parsing an XML file."))
97+
XMLDocument(p)
98+
end
99+
93100
function parse_string(s::AbstractString)
94101
p = ccall((:xmlParseMemory,libxml2), Xptr, (Xstr, Cint), s, sizeof(s) + 1)
95102
p != C_NULL || throw(XMLParseError("Failure in parsing an XML string."))

test/parse.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ docstr = """
2121
</bookstore>
2222
"""
2323

24-
xdoc = parse_string(docstr)
24+
for xdoc = (parse_string(docstr),
25+
parse_file(joinpath(dirname(@__FILE__), "ex1.xml")),
26+
parse_file(joinpath(dirname(@__FILE__), "ex1.xml"), C_NULL, 64)) # 64 == XML_PARSE_NOWARNING
2527

2628
@test version(xdoc) == "1.0"
2729
@test encoding(xdoc) == "UTF-8"
@@ -152,3 +154,4 @@ cz = find_element(xb1, "abc")
152154
@test is(cz, nothing)
153155

154156
free(xdoc)
157+
end

0 commit comments

Comments
 (0)