Giter Club home page Giter Club logo

xmldict.jl's Introduction

XMLDict.jl

XMLDict implements an Associative interface (get(), getindex(), haskey()) for reading XML elements and attributes. Requires EzXML.jl.

Build Status

Examples

Extract the content of a single tag...

xml = parse_xml("""
<CreateQueueResponse>
    <CreateQueueResult>
        <QueueUrl>http://queue.amazonaws.com/123456789012/testQueue</QueueUrl>
    </CreateQueueResult>
</CreateQueueResponse>
""")

@test xml["CreateQueueResult"]["QueueUrl"] == "http://queue.amazonaws.com/123456789012/testQueue"

Extract an attribute from a tag by using a :symbol as key...

xml = parse_xml("""
<bookstore>
  <book category="COOKING" tag="first"/>
<bookstore>
""")

@test xml["bookstore"]["book"][:category] == "COOKING"

Extract a list of tag content...

xml = parse_xml("""
<ListAllMyBucketsResult>
  <Buckets>
    <Bucket><Name>quotes</Name><CreationDate>2006-02-03T16:45:09.000Z</CreationDate></Bucket>
    <Bucket><Name>samples</Name><CreationDate>2006-02-03T16:41:58.000Z</CreationDate></Bucket>
  </Buckets>
</ListAllMyBucketsResult>
""")

@test [b["Name"] for b in xml["Buckets"]["Bucket"]] == ["quotes", "samples"]

Extract a dictionary of <Name>, <Value> tags content...

xml = parse_xml("""
<GetQueueAttributesResponse>
  <GetQueueAttributesResult>
    <Attribute><Name>VisibilityTimeout</Name><Value>30</Value></Attribute>
    <Attribute><Name>CreatedTimestamp</Name><Value>1286771522</Value></Attribute>
    <Attribute><Name>MaximumMessageSize</Name><Value>8192</Value></Attribute>
    <Attribute><Name>MessageRetentionPeriod</Name><Value>345600</Value></Attribute>
  </GetQueueAttributesResult>
</GetQueueAttributesResponse>
""")

d = [a["Name"] => a["Value"] for a in xml["GetQueueAttributesResult"]["Attribute"]]

Dict with 4 entries:
  "MessageRetentionPeriod" => "345600"
  "MaximumMessageSize"     => "8192"
  "VisibilityTimeout"      => "30"
  "CreatedTimestamp"       => "1286771522"

Convert entire XML document to a Julia Dict...

xml_string="""
<?xml version="1.0" encoding="UTF-8"?>
<bookstore brand="amazon">
  <book category="COOKING" tag="first">
    <title lang="en">
        Everyday Italian
    </title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
    <extract copyright="NA">The <b>bold</b> word is <b><i>not</i></b> <i>italic</i>.</extract>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
    <foo><![CDATA[<sender>John Smith</sender>]]></foo>
    <extract>Click <a href="foobar.com">right <b>here</b></a> for foobar.</extract>
  </book>
  <metadata>
       <foo>hello!</foo>
  </metadata>
</bookstore>
"""
d = xml_dict(xml_string)

Dict(
    :version=>"1.0",
    :encoding=>"UTF-8",
    "bookstore"=>Dict(
        :brand=>"amazon",
        "book"=>[
            Dict(
                :category=>"COOKING",
                :tag=>"first",
                "title"=>Dict(:lang=>"en",""=>"Everyday Italian"),
                "author"=>"Giada De Laurentiis",
                "year"=>"2005",
                "price"=>"30.00",
                "extract"=>Dict(
                    :copyright=>"NA",
                    ""=>["The ",Dict("b"=>"bold")," word is ", Dict("b"=>Dict("i"=>"not"))," ",Dict("i"=>"italic"),"."])
            ),
            Dict(
                :category=>"CHILDREN",
                "title"=>Dict(:lang=>"en",""=>"Harry Potter"),
                "author"=>"J K. Rowling",
                "year"=>"2005",
                "price"=>"29.99",
                "foo"=>"<sender>John Smith</sender>",
                "extract"=>["Click ",Dict("a"=>Dict(:href=>"foobar.com",""=>["right ",Dict("b"=>"here")]))," for foobar."]
            )],
        "metadata"=>Dict("foo"=>"hello!")
    )
)

xmldict.jl's People

Contributors

ararslan avatar femtotrader avatar freeboson avatar juliatagbot avatar mattbrzezinski avatar neomatrixcode avatar omus avatar samoconnor avatar staticfloat avatar stevengj avatar tkelman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xmldict.jl's Issues

Unable to parse one-layer deep strings

I recently came across this problem when trying to get the location of an AWS S3 Bucket. XMLDict appears to not be able to parse one-layer deep strings (for lack of better terminology?)

using XMLDict

xml_response = """
           <?xml version="1.0" encoding="UTF-8"?>
           <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">eu-west 1</LocationConstraint>"""

parse_xml(xml_response)
XMLDict.XMLDictElement with 0 entries

However, if the xml_response is two-layers deep it works fine:

using XMLDict

 xml_response = """
           <?xml version="1.0" encoding="UTF-8"?>
           <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Location>eu-west 1</Location></LocationConstraint>"""

parse_xml(xml_response)
XMLDict.XMLDictElement with 1 entry:
  "Location" => "eu-west 1"

Free xml doc

For large documents, it may be worth calling free(xml). I've been experimenting with this and notice the memory usage steadily increasing with larger files.

doesn't support keys, etc.

Currently, it XMLDict doesn't seem to support lots of the Associative interface. e.g. keys(xml) doesn't work.

Remove Compat?

@ararslan I assume the dependancy on Compat introduced by #11 is no longer needed now that 0.6 support is dropped?

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.