<reponame>morrigan-dev/python-examples
'''
Created on 17.03.2020

In diesem Modul sind Beispiele zu folgenden Themen enthalten:
- XML
- Web Services
- JSON

@author: morrigan
@see: https://www.py4e.com/html3/13-web
'''
import xml.etree.ElementTree as ET
import json

# Ein XML Format parsen
data = """
<person>
  <name>Chuck</name>
  <phone type="intl">
    +1 734 303 4456
  </phone>
  <email hide="yes" />
</person>"""

tree = ET.fromstring(data)
print("Name:", tree.find("name").text)
print("Attr:", tree.find("email").get("hide"))
print()

# Knoten einer XML durchlaufen
input = '''
<stuff>
  <users>
    <user x="2">
      <id>001</id>
      <name>Chuck</name>
    </user>
    <user x="7">
      <id>009</id>
      <name>Brent</name>
    </user>
  </users>
</stuff>'''

stuff = ET.fromstring(input)
lst = stuff.findall('users/user')
print('User count:', len(lst))

for item in lst:
    print('Name', item.find('name').text)
    print('Id', item.find('id').text)
    print('Attribute', item.get('x'))
print()

input = '''
<stuff>
  <users>
    <user x="2">
      <id>001</id>
      <name>Chuck</name>
    </user>
    <user x="7">
      <id>009</id>
      <name>Brent</name>
    </user>
  </users>
</stuff>'''

stuff = ET.fromstring(input)

lst = stuff.findall('users/user')
print('User count:', len(lst))

lst2 = stuff.findall('user')
print('User count:', len(lst2))
print()

# JSON parsen
data = '''
[
  { "id" : "001",
    "x" : "2",
    "name" : "Chuck"
  } ,
  { "id" : "009",
    "x" : "7",
    "name" : "Brent"
  }
]'''

info = json.loads(data)
print('User count:', len(info))

for item in info:
    print('Name', item['name'])
    print('Id', item['id'])
    print('Attribute', item['x'])
print()
'''
Created on 17.03.2020

In diesem Modul sind Beispiele zu folgenden Themen enthalten:
- XML
- Web Services
- JSON

@author: morrigan
@see: https://www.py4e.com/html3/13-web
'''
import xml.etree.ElementTree as ET
import json

# Ein XML Format parsen
data = """
<person>
  <name>Chuck</name>
  <phone type="intl">
    +1 734 303 4456
  </phone>
  <email hide="yes" />
</person>"""

tree = ET.fromstring(data)
print("Name:", tree.find("name").text)
print("Attr:", tree.find("email").get("hide"))
print()

# Knoten einer XML durchlaufen
input = '''
<stuff>
  <users>
    <user x="2">
      <id>001</id>
      <name>Chuck</name>
    </user>
    <user x="7">
      <id>009</id>
      <name>Brent</name>
    </user>
  </users>
</stuff>'''

stuff = ET.fromstring(input)
lst = stuff.findall('users/user')
print('User count:', len(lst))

for item in lst:
    print('Name', item.find('name').text)
    print('Id', item.find('id').text)
    print('Attribute', item.get('x'))
print()

input = '''
<stuff>
  <users>
    <user x="2">
      <id>001</id>
      <name>Chuck</name>
    </user>
    <user x="7">
      <id>009</id>
      <name>Brent</name>
    </user>
  </users>
</stuff>'''

stuff = ET.fromstring(input)

lst = stuff.findall('users/user')
print('User count:', len(lst))

lst2 = stuff.findall('user')
print('User count:', len(lst2))
print()

# JSON parsen
data = '''
[
  { "id" : "001",
    "x" : "2",
    "name" : "Chuck"
  } ,
  { "id" : "009",
    "x" : "7",
    "name" : "Brent"
  }
]'''

info = json.loads(data)
print('User count:', len(info))

for item in info:
    print('Name', item['name'])
    print('Id', item['id'])
    print('Attribute', item['x'])
print()
Created on 17.03.2020  In diesem Modul sind Beispiele zu folgenden Themen enthalten: - XML - Web Services - JSON  @author: morrigan @see: https://www.py4e.com/html3/13-web # Ein XML Format parsen <person>   <name>Chuck</name>   <phone type="intl">     +1 734 303 4456   </phone>   <email hide="yes" /> </person> # Knoten einer XML durchlaufen <stuff>   <users>     <user x="2">       <id>001</id>       <name>Chuck</name>     </user>     <user x="7">       <id>009</id>       <name>Brent</name>     </user>   </users> </stuff> <stuff>   <users>     <user x="2">       <id>001</id>       <name>Chuck</name>     </user>     <user x="7">       <id>009</id>       <name>Brent</name>     </user>   </users> </stuff> # JSON parsen [   { "id" : "001",     "x" : "2",     "name" : "Chuck"   } ,   { "id" : "009",     "x" : "7",     "name" : "Brent"   } ]