0

I am looking for solution to my problem related to XML in python. i have an xml file

-<Group name="OptionalObjects">
 -<Array name="Pre-defined Error Field">
    <Index>4099</Index>
    <ObjectType>8</ObjectType>
  -<Variable name="Number of Errors">
      <Index>4099</Index>
      <Subindex>0</Subindex>
      <DataType>5</DataType>
      <AccessType>2</AccessType>
      <ObjectType>7</ObjectType>
      <DefaultValue>1</DefaultValue>
      <PDOMapping>0</PDOMapping>
   </Variable>
  +<Variable name="Standard Error Field">
 </Array>

This is just a part from hole code. i must search for specific index in whole codde and the program must print me out a parents of that index and all childrens of that parent includ with index ( Index,Subindex,DataType...PDOMapping). please hellp me becouse i'm totaly lost. i was trying with for loop but there is no futer. tnx

2 Answers2

0

Parsing xml is a relatively standard problem, you can have a look at this: https://docs.python.org/2/library/xml.etree.elementtree.html

Alternatively, BeautifulSoup is a nice library to parse html and xml: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html

Some more info about BeautifulSoup in a previous question: Python BeautifulSoup XML Parsing

Community
  • 1
  • 1
etna
  • 1,083
  • 7
  • 13
0

Thanks...

for parsing xml i use recurse becous is better

import xml.etree.ElementTree as etree
tree = etree.parse('asd.xml')
root = tree.getroot()

def recurse_children(node):
    for children in node:
         if len(children) > 0:
            recurse_children(children)
        print children.tag, children.attrib, children.text
    print ('\n')

for child in root:
    recurse_children(child)

I looking in this BeautifulSoup from yesterday but still don't get it... is anyone that could help?