@@ -518,7 +518,7 @@ Base.show(io::IO, o::Node) = _show_node(io, o)
518518
519519# -----------------------------------------------------------------------------# Node Constructors
520520# auto-detect how to create a Node
521- _node(x; depth= - 1 ) = Node(text( string(x) ); depth)
521+ _node(x; depth= - 1 ) = Node(nodetype = TEXT, value = string(x); depth)
522522_node(x:: Node ; depth= x. depth) = Node(x; depth)
523523
524524module NodeConstructors
@@ -530,26 +530,69 @@ export document, dtd, declaration, processing_instruction, comment, cdata, text,
530530
531531attrs(kw) = OrderedDict{String,String}(string(k) => string(v) for (k,v) in kw)
532532
533- document(children:: Vector{Node} ) = Node(nodetype= DOCUMENT, children)
534- document(children:: Node... ) = document(Node.(children))
533+ """
534+ document(children::Vector{Node})
535+ document(children::Node...)
536+ """
537+ document(children:: Vector{Node} ) = Node(;nodetype= DOCUMENT, children)
538+ document(children:: Node... ) = document(collect(children))
535539
540+ """
541+ dtd(value::AbstractString)
542+ """
536543dtd(value:: AbstractString ) = Node(nodetype= DTD, value= String(value))
537544
545+ """
546+ declaration(; attributes...)
547+ """
538548declaration(attributes:: OrderedDict{String,String} ) = Node(;nodetype= DECLARATION, attributes)
539549declaration(; kw... ) = declaration(attrs(kw))
540550
551+ """
552+ processing_instruction(tag::AbstractString; attributes...)
553+ """
541554processing_instruction(tag, attributes:: OrderedDict{String,String} ) = Node(;nodetype= PROCESSING_INSTRUCTION, tag= string(tag), attributes)
542555processing_instruction(tag; kw... ) = processing_instruction(tag, attrs(kw))
543556
557+ """
558+ comment(value::AbstractString)
559+ """
544560comment(value:: AbstractString ) = Node(nodetype= COMMENT, value= String(value))
545561
562+ """
563+ cdata(value::AbstractString)
564+ """
546565cdata(value:: AbstractString ) = Node(nodetype= CDATA, value= String(value))
547566
567+ """
568+ text(value::AbstractString)
569+ """
548570text(value:: AbstractString ) = Node(nodetype= TEXT, value= String(value))
549571
572+ """
573+ element(tag::AbstractString, children::Vector{Node}; attributes...)
574+ element(tag::AbstractString, children::Node...; attributes...)
575+
576+ Example:
550577
551- element(tag, children... ; kw... ) = Node(; nodetype= ELEMENT, tag= string(tag), attributes= attrs(kw))(children... )
552- Base. getproperty(:: typeof (element), tag:: Symbol ) = element(string(tag))
578+ using XML.NodeConstructors
579+
580+ n = element("tag", "child"; key="value")
581+ # Node ELEMENT <tag key="value"> (1 child)
582+
583+ only(n)
584+ # Node TEXT "child"
585+
586+ push!(n, cdata("hello > < ' \" I have odd characters"))
587+
588+ children(n)
589+ # 2-element Vector{Node}:
590+ # Node TEXT "child"
591+ # Node CDATA <![CDATA[hello > < ' " I have odd characters]]>
592+ """
593+ element(tag, children... ; kw... ) = Node(; nodetype= ELEMENT, tag= string(tag), attributes= attrs(kw))(_node.(children). .. )
594+ element(tag, children:: Vector{Node} ; kw... ) = element(tag, children... ; kw... )
595+ # Base.getproperty(::typeof(element), tag::Symbol) = element(string(tag))
553596end
554597
555598
0 commit comments