Skip to content

Commit

Permalink
add xml to json
Browse files Browse the repository at this point in the history
  • Loading branch information
C-o-m-o-n committed Dec 30, 2023
1 parent 3bbd4ba commit e5a97ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
26 changes: 26 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ <h3>Convert JSON and CSV</h3>
#id,name
#12,Collins

</code>
</pre>
</div>

<h3>Convert JSON and xml</h3>
<div >
<pre>
<code>
from textbin.textbin import *
textbin_obj = textbin.Textbin()

#convert json to xml
json_data = { 'id' : 12 , 'name' : 'Collins' }
converted = textbin_obj.json_to_xml(json_data)
print(converted)

#Output <item><id>12</id><name>Collins</name></item>

#convert xml to json
xml_data = "<item><id>12</id><name>Collins</name></item>"
converted = textbin_obj.xml_to_json(xml_data)
print(converted)
print(converted)

#Output { 'id' : 12 , 'name' : 'Collins' }

</code>
</pre>
</div>
Expand Down
11 changes: 9 additions & 2 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@

#convert json to xml
print("convert json to xml")
json_list = [{ 'id' : 12 , 'name' : 'Collins' }]
converted = textbin_obj.json_to_xml(json_list)
json_data = { 'id' : 12 , 'name' : 'Collins' }
converted = textbin_obj.json_to_xml(json_data)
print(converted)

print("************************************************************\n")

#convert xml to json
print("convert xml to json")
xml_data = "<item><id>12</id><name>Collins</name></item>"
converted = textbin_obj.xml_to_json(xml_data)
print(converted)

print("************************************************************\n")
Binary file modified pytextbin/__pycache__/pytextbin.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion pytextbin/pytextbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _element_to_dict(self, element: ET.Element) -> Union[dict, list]:
if element:
if element.attrib:
return {element.tag: element.attrib}
children = element.getchildren()
children = element.get(self)
if children:
out = {}
for child in children:
Expand Down

0 comments on commit e5a97ef

Please sign in to comment.