-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module XMLJSONExt | ||
|
||
using JSON | ||
using OrderedCollections | ||
using XML | ||
|
||
function XML.xml2dicts(node::Node) | ||
if nodetype(node) == XML.Document | ||
# root node has no tag and 1 child, so it is special, just apply to its child | ||
return XML.xml2dicts(only(node.children)) | ||
elseif nodetype(node) == XML.Text | ||
# text nodes have no tag, and just have contents | ||
return OrderedDict("_" => node.value) | ||
elseif nodetype(node) == XML.Element | ||
# normal case | ||
dict = OrderedDict{String,Any}() | ||
# first put in the attributes | ||
if !isnothing(attributes(node)) | ||
merge!(dict, attributes(node)) | ||
end | ||
# then any children | ||
for child in children(node) | ||
child_result = XML.xml2dicts(child) | ||
for (key, value) in child_result | ||
if haskey(dict, key) | ||
if isa(dict[key], Vector) | ||
push!(dict[key], value) | ||
else | ||
dict[key] = [dict[key], value] | ||
end | ||
else | ||
dict[key] = value | ||
end | ||
end | ||
end | ||
return OrderedDict(tag(node) => dict) | ||
else | ||
throw(DomainError(nodetype(node), "unsupported node type")) | ||
end | ||
end | ||
|
||
|
||
|
||
function XML.xml2json(xml::Node, json="") | ||
dict_result = XML.xml2dicts(xml) | ||
|
||
if isdir(dirname(json)) | ||
open(json, "w") do io | ||
JSON.print(io, dict_result, 2) | ||
end | ||
else | ||
return JSON.json(dict_result) | ||
end | ||
end | ||
|
||
XML.xml2json(xml::IO, json="") = XML.xml2json(read(xml, String), json) | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using JSON | ||
|
||
@testset "XML to JSON" begin | ||
xml = read("data/toJSON.xml", Node) | ||
json = xml2json(xml) | ||
d = xml2dicts(xml) | ||
@test JSON.parse(json) == d | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[deps] | ||
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" | ||
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" | ||
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<instance | ||
format="XCSP3" | ||
type="CSP"> | ||
<variables> | ||
<var id="x">0 1</var> | ||
<var id="y">0 1</var> | ||
<var id="z">0 1</var> | ||
</variables> | ||
<constraints> | ||
<extension> | ||
<list>x y</list> | ||
<supports>(0,0) (1,1)</supports> | ||
</extension> | ||
<extension> | ||
<list>x z</list> | ||
<supports>(0,0) (1,1)</supports> | ||
</extension> | ||
<extension> | ||
<list>y z</list> | ||
<supports>(0,1) (1,0)</supports> | ||
</extension> | ||
</constraints> | ||
</instance> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters