Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nhu313/Quinn
XML parser for Elixir
https://github.com/nhu313/Quinn
Last synced: 7 days ago
JSON representation
XML parser for Elixir
- Host: GitHub
- URL: https://github.com/nhu313/Quinn
- Owner: nhu313
- License: other
- Created: 2014-07-14T04:42:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-25T15:43:22.000Z (almost 6 years ago)
- Last Synced: 2024-10-30T16:56:29.569Z (9 days ago)
- Language: Elixir
- Size: 131 KB
- Stars: 49
- Watchers: 2
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - XML parser for Elixir. (XML)
- fucking-awesome-elixir - quinn - XML parser for Elixir. (XML)
- awesome-elixir - quinn - XML parser for Elixir. (XML)
README
Quinn
=====A simple XML parser in Elixir aims to parse rss/atom feeds. I'm currently
using `xmerl_scan.string` to parse the xml. I'm a bit busy so I may take awhile to response and/or update. Feel free to send a pull request. Otherwise, check out [hex](https://hex.pm/packages?search=xml&sort=downloads) for other XML parsers that may be better maintained.# Parsing
```elixir
Quinn.parse("YahooBing")
```
Calling parse on the xml will produce
```elixir
[%{attr: [], name: :head,
value: [%{attr: [short_name: "yah"], name: :title, value: ["Yahoo"]},
%{attr: [], name: :"title:content", value: ["Bing"]}]}]
```
### Parsing without namespaces(key prefix)
```elixir
xml = "
Title
Description
"Quinn.parse(xml, %{strip_namespaces: true})
```Calling parse on the xml will produce
```elixir
[%{attr: ["xsi:type": "d4p1:Answer"],
name: :return,
value: [%{attr: [], name: :title, value: ["Title"]},
%{attr: [], name: :description, value: ["Description"]}]}]
```### Parsing comments
```elixir
xml = ~s(Yahoo)
result = Quinn.parse(xml, %{comments: true})
```
The xml above will give you this. Note the name is `comments`.```elixir
[%{attr: [],
name: :head,
value: [%{attr: [short_name: "yah"], name: :title, value: ["Yahoo"]},
%{attr: [], name: :comments, value: ~s(- )}]}]
```### Parsing - Attributes as map
```elixir
xml = ~s( Title Description )result = Quinn.parse(xml, %{map_attributes: true})
```The xml above will give you the attributes in map
```
expected = [%{attr: %{"xsi:type": "d4p1:Answer", desc: "bla"},
name: :"m:return",
value: [%{attr: %{}, name: :"d4p1:Title", value: ["Title"]},
%{attr: %{},
name: :"d4p1:Description",
value: ["Description"]}]}]
```
# Finding nodesSuppose you want to find all the body nodes from this structure:
```elixir
structure = %{attr: [],
name: :html,
value: [%{attr: [], name: :head, value: ["title"]},
%{attr: [], name: :title, value: []},
%{attr: [], name: :body, value: ["body1", "body2"]},
%{attr: [], name: :footer, value: [%{attr: [], name: :line, value: ["this"]}]},
%{attr: [], name: :body, value: [%{attr: [], name: :line, value: ["that"]}]},
%{attr: [], name: :"content:encoded", value: ["comet!!
"]}]}
```
You can call
```elixir
Quinn.find(structure, :body)
```
This will be the result:
```elixir
[%{attr: [], name: :body, value: ["body1", "body2"]},
%{attr: [], name: :body, value: [%{attr: [], name: :line, value: ["that"]}]}]
```
Or given the structure above, you want to find the node `line` inside `body`, then you can invoke it like this:
```elixir
Quinn.find(structure, [:body, :line])
```
The result will be
```elixir
[%{attr: [], name: :line, value: ["that"]}]
```
Please refer to the tests if you want to see more example on how it is used.Please let me know if you come across any problem. I'm still new to Elixir so feel free to contribute or clean up the code.
# License
Quinn source code is released under Apache 2 License. Check LICENSE file for more information.