Skip to content

Commit 572d69d

Browse files
committed
print_tree for Node
1 parent 27154c7 commit 572d69d

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ doc.root.id = "A new attribute called `id`"
3131
write("newfile.xml", doc)
3232
```
3333

34-
## Internals
35-
3634
### Types
3735

3836
```julia

src/XML.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function Base.:(==)(a::T, b::T) where {T <: AbstractXMLNode}
115115
all(getfield(a, f) == getfield(b, f) for f in fieldnames(T))
116116
end
117117

118-
const INDENT = " "
118+
const INDENT = " "
119119

120120
showxml(x; depth=0) = (io=IOBuffer(); showxml(io, x); print(String(take!(io))))
121121

src/node.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function showxml(io::IO, o::Node; depth=0)
8787
if isnothing(o.children)
8888
p(" />")
8989
else
90-
if length(o.children) == 1 && o.children[1] isa String
90+
if length(o.children) == 1 && o.children[1] isa String && !occursin('\n', o.children[1])
9191
p('>')
9292
showxml(io, o.children[1])
9393
p("</", o.tag, '>')
@@ -115,6 +115,24 @@ function Node(itr::XMLTokenIterator)
115115
return doc
116116
end
117117

118+
function printnode(io::IO, o::Node)
119+
p(args...) = print(io, args...)
120+
if o.type == DOCUMENT_NODE
121+
p("DOCUMENT")
122+
elseif o.type == DTD_NODE
123+
p("<!doctype ", o.children, '>')
124+
elseif o.type == DECLARATION_NODE
125+
p("<?", o.tag, attr_string(o), "?>")
126+
elseif o.type == COMMENT_NODE
127+
p("<!-- ", o.children, " -->")
128+
elseif o.type == CDATA_NODE
129+
p("<![CDATA[", o.children, "]]>")
130+
elseif o.type == ELEMENT_NODE
131+
p('<', o.tag, attr_string(o))
132+
end
133+
o.type != DOCUMENT_NODE && p('>')
134+
end
135+
children(o::Node) = o.type in [ELEMENT_NODE, DOCUMENT_NODE] ? o.children : ()
118136

119137
readnode(file::String) = open(io -> Node(XMLTokenIterator(io)), file, "r")
120138

@@ -136,7 +154,7 @@ function add_children!(e::Node, o::XMLTokenIterator, until::String)
136154
add_children!(child, o, "</$(child.tag)>")
137155
push!(c, child)
138156
elseif T == TEXTTOKEN
139-
push!(c, unescape(s))
157+
push!(c, unescape(rstrip(s)))
140158
elseif T == DTDTOKEN
141159
push!(c, dtd(replace(s, "<!doctype " => "", "<!DOCTYPE " => "", '>' => "")))
142160
elseif T == DECLARATIONTOKEN

0 commit comments

Comments
 (0)