Git Product home page Git Product logo

ezxml.jl's Introduction

EzXML.jl Logo

EzXML.jl - XML/HTML tools for primates

Docs Stable Docs Latest CI Status codecov.io

EzXML.jl is a package to handle XML/HTML documents for primates.

The main features are:

  • Reading and writing XML/HTML documents.
  • Traversing XML/HTML trees with DOM interfaces.
  • Searching elements using XPath.
  • Proper namespace handling.
  • Capturing error messages.
  • Automatic memory management.
  • Document validation.
  • Streaming parsing for large XML files.

Installation

Install EzXML.jl as follows:

julia -e 'using Pkg; Pkg.add("EzXML")'

This package depends on libxml2, which will be automatically installed as an artifact via XML2_jll.jl if you use Julia 1.3 or later. Currently, Windows, Linux, macOS, and FreeBSD are now supported.

Version compatibility

EzXML.jl Julia
1.0 1.0 or later
1.1 1.3 or later

Usage

# Load the package.
using EzXML

# Parse an XML string
# (use `readxml(<filename>)` to read a document from a file).
doc = parsexml("""
<primates>
    <genus name="Homo">
        <species name="sapiens">Human</species>
    </genus>
    <genus name="Pan">
        <species name="paniscus">Bonobo</species>
        <species name="troglodytes">Chimpanzee</species>
    </genus>
</primates>
""")

# Get the root element from `doc`.
primates = root(doc)  # or `doc.root`

# Iterate over child elements.
for genus in eachelement(primates)
    # Get an attribute value by name.
    genus_name = genus["name"]
    println("- ", genus_name)
    for species in eachelement(genus)
        # Get the content within an element.
        species_name = nodecontent(species)  # or `species.content`
        println("  โ”” ", species["name"], " (", species_name, ")")
    end
end
println()

# Find texts using XPath query.
for species_name in nodecontent.(findall("//species/text()", primates))
    println("- ", species_name)
end

Quick reference

See the reference page or docstrings for more details.

Types:

  • EzXML.Document: an XML/HTML document
  • EzXML.Node: an XML/HTML node including elements, attributes, texts, etc.
  • EzXML.XMLError: an error happened in libxml2
  • EzXML.StreamReader: a streaming XML reader

IO:

  • From file: readxml(filename|stream), readhtml(filename|stream)
  • From string or byte array: parsexml(string), parsehtml(string)
  • To file: write(filename, doc)
  • To stream: print(io, doc)

Accessors:

  • Node information: nodetype(node), nodepath(node), nodename(node), nodecontent(node), setnodename!(node, name), setnodecontent!(node, content)
  • Node property: node.type, node.name, node.path, node.content, node.namespace
  • Document:
    • Property: version(doc), encoding(doc), hasversion(doc), hasencoding(doc)
    • Node: root(doc), dtd(doc), hasroot(doc), hasdtd(doc), setroot!(doc, element_node), setdtd!(doc, dtd_node)
  • Document property: doc.version, doc.encoding, doc.node, doc.root, doc.dtd
  • Attributes: node[name], node[name] = value, haskey(node, name), delete!(node, name)
  • Node predicate:
    • Document: hasdocument(node)
    • Parent: hasparentnode(node), hasparentelement(node)
    • Child: hasnode(node), haselement(node)
    • Sibling: hasnextnode(node), hasprevnode(node), hasnextelement(node), hasprevelement(node)
    • Node type: iselement(node), isattribute(node), istext(node), iscdata(node), iscomment(node), isdtd(node)
  • Tree traversal:
    • Document: document(node)
    • Parent: parentnode(node), parentelement(node)
    • Child: firstnode(node), lastnode(node), firstelement(node), lastelement(node)
    • Sibling: nextnode(node), prevnode(node), nextelement(node), prevelement(node)
  • Tree modifiers:
    • Link: link!(parent_node, child_node), linknext!(target_node, node), linkprev!(target_node, node)
    • Unlink: unlink!(node)
    • Create: addelement!(parent_node, name, [content])
  • Iterators:
    • Iterator: eachnode(node), eachelement(node), eachattribute(node)
    • Vector: nodes(node), elements(node), attributes(node)
  • Counters: countnodes(node), countelements(node), countattributes(node)
  • Namespaces: namespace(node), namespaces(node)

Constructors:

  • EzXML.Document type: XMLDocument(version="1.0"), HTMLDocument(uri=nothing, externalID=nothing)
  • EzXML.Node type: XMLDocumentNode(version="1.0"), HTMLDocumentNode(uri, externalID), ElementNode(name), TextNode(content), CommentNode(content), CDataNode(content), AttributeNode(name, value), DTDNode(name, [systemID, [externalID]])

Queries:

  • XPath: findall(xpath, doc|node), findfirst(xpath, doc|node), findlast(xpath, doc|node)

Examples

Other XML/HTML packages in Julia

ezxml.jl's People

Contributors

bicycle1885 avatar nishya avatar ararslan avatar clarkevans avatar omus avatar staticfloat avatar felipenoris avatar xiuliren avatar juliatagbot avatar kescobo avatar lcw avatar visr avatar mortenpi avatar musm avatar programmeroftheeve avatar yonghee-kim avatar ultradian avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.