programing

XML을 해석하고 특정 노드 속성의 인스턴스를 가져오려면 어떻게 해야 합니까?

firstcheck 2022. 10. 28. 21:35
반응형

XML을 해석하고 특정 노드 속성의 인스턴스를 가져오려면 어떻게 해야 합니까?

XML에는 여러 행이 있으며 특정 노드 속성의 인스턴스를 가져오려고 합니다.

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

에는 어떻게 할 수 ?foobar에서는 「」를 사용하고 싶다"1" ★★★★★★★★★★★★★★★★★」"2".

제안합니다.같은 API에 호환성이 있는 다른 실장(및 등)이 있습니다.cElementTree 이하는 것은 더 은 Python의 .ElementTree를 정의합니다.

인스턴스를 .root예를 들어 XML 함수를 사용하거나 다음과 같은 파일을 구문 분석하여 XML에서 추출합니다.

import xml.etree.ElementTree as ET
root = ET.parse('thefile.xml').getroot()

또는 에 나타내는 다른 많은 방법 중 하나를 사용할 수 있습니다.그런 다음 다음과 같은 작업을 수행합니다.

for type_tag in root.findall('bar/type'):
    value = type_tag.get('foobar')
    print(value)

출력:

1
2

minidom 가장 빠르고 직설적입니다.

XML:

<data>
    <items>
        <item name="item1"></item>
        <item name="item2"></item>
        <item name="item3"></item>
        <item name="item4"></item>
    </items>
</data>

Python:

from xml.dom import minidom

dom = minidom.parse('items.xml')
elements = dom.getElementsByTagName('item')

print(f"There are {len(elements)} items:")

for element in elements:
    print(element.attributes['name'].value)

출력:

There are 4 items:
item1
item2
item3
item4

Beautiful Soup을 사용할 수 있습니다.

from bs4 import BeautifulSoup

x="""<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>"""

y=BeautifulSoup(x)
>>> y.foo.bar.type["foobar"]
u'1'

>>> y.foo.bar.findAll("type")
[<type foobar="1"></type>, <type foobar="2"></type>]

>>> y.foo.bar.findAll("type")[0]["foobar"]
u'1'
>>> y.foo.bar.findAll("type")[1]["foobar"]
u'2'

많은 옵션이 있습니다.cElement속도와 메모리 사용률이 문제가 되는 경우는, 트리가 올바르게 표시됩니다.단순히 파일을 읽는 것에 비해 오버헤드는 거의 없습니다.readlines.

관련 메트릭은 cElement에서 복사한 다음 표에 기재되어 있습니다.트리 웹 사이트:

library                         time    space
xml.dom.minidom (Python 2.1)    6.3 s   80000K
gnosis.objectify                2.0 s   22000k
xml.dom.minidom (Python 2.4)    1.4 s   53000k
ElementTree 1.2                 1.6 s   14500k  
ElementTree 1.2.4/1.3           1.1 s   14500k  
cDomlette (C extension)         0.540 s 20500k
PyRXPU (C extension)            0.175 s 10850k
libxml2 (C extension)           0.098 s 16000k
readlines (read as utf-8)       0.093 s 8850k
cElementTree (C extension)  --> 0.047 s 4900K <--
readlines (read as ascii)       0.032 s 5050k   

@jfs가 지적한 바와 같이cElementTreePython ★★★★★★★★★★★★★★★★★★★★★★.

  • Python 2:from xml.etree import cElementTree as ElementTree.
  • Python 3:from xml.etree import ElementTree(액셀러레이션 C 버전이 자동으로 사용됩니다).

단순성을 위해 xmltodict를 권장합니다.

XML을 Ordered Dict로 해석합니다.

>>> e = '<foo>
             <bar>
                 <type foobar="1"/>
                 <type foobar="2"/>
             </bar>
        </foo> '

>>> import xmltodict
>>> result = xmltodict.parse(e)
>>> result

OrderedDict([(u'foo', OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))]))])

>>> result['foo']

OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))])

>>> result['foo']['bar']

OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])])

lxml.objectify는 매우 간단합니다.

샘플 텍스트 가져오기:

from lxml import objectify
from collections import defaultdict

count = defaultdict(int)

root = objectify.fromstring(text)

for item in root.bar.type:
    count[item.attrib.get("foobar")] += 1

print dict(count)

출력:

{'1': 1, '2': 1}

Python에는 expat XML 파서에 대한 인터페이스가 있습니다.

xml.parsers.expat

검증되지 않은 파서이므로 부정한 XML은 검출되지 않습니다.그러나 파일이 정확하다는 것을 알게 되면, 이것은 매우 좋은 결과입니다.그리고 아마 원하는 정보를 얻을 수 있을 것이고 나머지는 즉시 폐기할 수 있습니다.

stringofxml = """<foo>
    <bar>
        <type arg="value" />
        <type arg="value" />
        <type arg="value" />
    </bar>
    <bar>
        <type arg="value" />
    </bar>
</foo>"""
count = 0
def start(name, attr):
    global count
    if name == 'type':
        count += 1

p = expat.ParserCreate()
p.StartElementHandler = start
p.Parse(stringofxml)

print count # prints 4

단순한 xml-to-python 객체 라이브러리이므로 untangle을 사용할 수 있습니다.다음은 예를 제시하겠습니다.

설치:

pip install untangle

사용방법:

XML 파일(약간 변경):

<foo>
   <bar name="bar_name">
      <type foobar="1"/>
   </bar>
</foo>

Attribute를 사용하여 untangle:

import untangle

obj = untangle.parse('/path_to_xml_file/file.xml')

print obj.foo.bar['name']
print obj.foo.bar.type['foobar']

출력은 다음과 같습니다.

bar_name
1

언앵글에 대한 자세한 내용은 "언앵글"을 참조하십시오.

또, XML 및 Python을 사용하는 툴의 리스트가 「Python and XML」에 기재되어 있습니다.또한 가장 일반적인 툴은 이전 답변에 기재되어 있는 것을 알 수 있습니다.

decxml을 제안할 수도 있어

완전 공개:ElementTree로 수십 줄의 필수 구문 분석/시리얼라이제이션 코드를 쓸 필요 없이 XML과 Python 데이터 구조를 변환할 수 있는 방법을 찾고 있었기 때문에 이 라이브러리를 작성했습니다.

decxml을 사용하면 프로세서를 사용하여 XML 문서의 구조와 XML과 Python 데이터 구조 간의 매핑 방법을 선언적으로 정의할 수 있습니다.프로세서는 시리얼라이제이션과 해석 양쪽뿐만 아니라 기본적인 검증 수준에도 사용됩니다.

Python 데이터 구조로의 해석은 간단합니다.

import declxml as xml

xml_string = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>
"""

processor = xml.dictionary('foo', [
    xml.dictionary('bar', [
        xml.array(xml.integer('type', attribute='foobar'))
    ])
])

xml.parse_from_string(processor, xml_string)

그러면 다음과 같은 출력이 생성됩니다.

{'bar': {'foobar': [1, 2]}}

같은 프로세서를 사용하여 데이터를 XML로 시리얼화할 수도 있습니다.

data = {'bar': {
    'foobar': [7, 3, 21, 16, 11]
}}

xml.serialize_to_string(processor, data, indent='    ')

그러면 다음과 같은 출력이 생성됩니다.

<?xml version="1.0" ?>
<foo>
    <bar>
        <type foobar="7"/>
        <type foobar="3"/>
        <type foobar="21"/>
        <type foobar="16"/>
        <type foobar="11"/>
    </bar>
</foo>

사전 대신 개체로 작업하려는 경우 개체 간에 데이터를 변환하는 프로세서를 정의할 수도 있습니다.

import declxml as xml

class Bar:

    def __init__(self):
        self.foobars = []

    def __repr__(self):
        return 'Bar(foobars={})'.format(self.foobars)


xml_string = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>
"""

processor = xml.dictionary('foo', [
    xml.user_object('bar', Bar, [
        xml.array(xml.integer('type', attribute='foobar'), alias='foobars')
    ])
])

xml.parse_from_string(processor, xml_string)

그러면 다음과 같은 출력이 생성됩니다.

{'bar': Bar(foobars=[1, 2])}

여기에서는, 매우 심플하지만, 효과적인 코드를cElementTree.

try:
    import cElementTree as ET
except ImportError:
  try:
    # Python 2.5 need to import a different module
    import xml.etree.cElementTree as ET
  except ImportError:
    exit_err("Failed to import cElementTree from any known place")      

def find_in_tree(tree, node):
    found = tree.find(node)
    if found == None:
        print "No %s in file" % node
        found = []
    return found  

# Parse a xml file (specify the path)
def_file = "xml_file_name.xml"
try:
    dom = ET.parse(open(def_file, "r"))
    root = dom.getroot()
except:
    exit_err("Unable to open and parse input definition file: " + def_file)

# Parse to find the child nodes list of node 'myNode'
fwdefs = find_in_tree(root,"myNode")

이것은 "python xml parse"에서 가져온 것입니다.

XML:

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

Python 코드:

import xml.etree.cElementTree as ET

tree = ET.parse("foo.xml")
root = tree.getroot() 
root_tag = root.tag
print(root_tag) 

for form in root.findall("./bar/type"):
    x=(form.attrib)
    z=list(x)
    for i in z:
        print(x[i])

출력:

foo
1
2

Lib 전용 API를 사용할 필요가 없습니다.python-benedictXML에서 새로운 인스턴스를 초기화하기만 하면 됩니다.이 인스턴스는,dict서브클래스

설치는 간단합니다.pip install python-benedict

from benedict import benedict as bdict

# data-source can be an url, a filepath or data-string (as in this example)
data_source = """
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>"""

data = bdict.from_xml(data_source)
t_list = data['foo.bar'] # yes, keypath supported
for t in t_list:
   print(t['@foobar'])

다음과 같은 다양한 형식으로 I/O 작업을 지원하고 정규화합니다.Base64,CSV,JSON,TOML,XML,YAML그리고.query-string.

GitHub에서 잘 테스트되고 오픈 소스입니다.공개:나는 작가다.

xml.etree 입니다.Element Tree vs. lxml

다음은 가장 많이 사용되는 라이브러리 중 하나를 선택하기 전에 알아두 가지 장점입니다.

xml.etree 입니다.Element Tree:

  1. 표준 라이브러리: 모듈 설치 불필요

lxml

  1. XML 선언을 쉽게 작성할 수 있습니다.예를 들어 추가가 필요한 경우standalone="no"?
  2. 예쁜 인쇄: 추가 코드 없이 멋진 들여쓰기 XML을 만들 수 있습니다.
  3. 기능 객체화:일반적인 Python 객체 계층을 다루는 것처럼 XML을 사용할 수 있습니다..node.
  4. sourceline를 사용하면 사용 중인 XML 요소의 행을 쉽게 얻을 수 있습니다.
  5. 내장 XSD 스키마 체커를 사용할 수도 있습니다.
import xml.etree.ElementTree as ET
data = '''<foo>
           <bar>
               <type foobar="1"/>
               <type foobar="2"/>
          </bar>
       </foo>'''
tree = ET.fromstring(data)
lst = tree.findall('bar/type')
for item in lst:
    print item.get('foobar')

그러면 다음 값이 출력됩니다.foobar기여하다.

simplified_scrapy: 새로운 lib를 사용하고 나서 사랑에 빠졌어요.당신에게 그것을 추천합니다.

from simplified_scrapy import SimplifiedDoc
xml = '''
<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>
'''

doc = SimplifiedDoc(xml)
types = doc.selects('bar>type')
print (len(types)) # 2
print (types.foobar) # ['1', '2']
print (doc.selects('bar>type>foobar()')) # ['1', '2']

여기 더 많은 예가 있다.이 lib는 사용하기 쉽다.

#If the xml is in the form of a string as shown below then
from lxml  import etree, objectify
'''sample xml as a string with a name space {http://xmlns.abc.com}'''
message =b'<?xml version="1.0" encoding="UTF-8"?>\r\n<pa:Process xmlns:pa="http://xmlns.abc.com">\r\n\t<pa:firsttag>SAMPLE</pa:firsttag></pa:Process>\r\n'  # this is a sample xml which is a string


print('************message coversion and parsing starts*************')

message=message.decode('utf-8') 
message=message.replace('<?xml version="1.0" encoding="UTF-8"?>\r\n','') #replace is used to remove unwanted strings from the 'message'
message=message.replace('pa:Process>\r\n','pa:Process>')
print (message)

print ('******Parsing starts*************')
parser = etree.XMLParser(remove_blank_text=True) #the name space is removed here
root = etree.fromstring(message, parser) #parsing of xml happens here
print ('******Parsing completed************')


dict={}
for child in root: # parsed xml is iterated using a for loop and values are stored in a dictionary
    print(child.tag,child.text)
    print('****Derving from xml tree*****')
    if child.tag =="{http://xmlns.abc.com}firsttag":
        dict["FIRST_TAG"]=child.text
        print(dict)


### output
'''************message coversion and parsing starts*************
<pa:Process xmlns:pa="http://xmlns.abc.com">

    <pa:firsttag>SAMPLE</pa:firsttag></pa:Process>
******Parsing starts*************
******Parsing completed************
{http://xmlns.abc.com}firsttag SAMPLE
****Derving from xml tree*****
{'FIRST_TAG': 'SAMPLE'}'''

외부 라이브러리 또는 서드파티 도구를 사용하지 않으려면 아래 코드를 사용해 보십시오.

  • 해석됩니다.xmldictionary
  • xml 속성도 해석합니다.
  • 빈 됩니다.<tag/>에는 '태그'라는 요.<tag var=val/>

코드

import re

def getdict(content):
    res=re.findall("<(?P<var>\S*)(?P<attr>[^/>]*)(?:(?:>(?P<val>.*?)</(?P=var)>)|(?:/>))",content)
    if len(res)>=1:
        attreg="(?P<avr>\S+?)(?:(?:=(?P<quote>['\"])(?P<avl>.*?)(?P=quote))|(?:=(?P<avl1>.*?)(?:\s|$))|(?P<avl2>[\s]+)|$)"
        if len(res)>1:
            return [{i[0]:[{"@attributes":[{j[0]:(j[2] or j[3] or j[4])} for j in re.findall(attreg,i[1].strip())]},{"$values":getdict(i[2])}]} for i in res]
        else:
            return {res[0]:[{"@attributes":[{j[0]:(j[2] or j[3] or j[4])} for j in re.findall(attreg,res[1].strip())]},{"$values":getdict(res[2])}]}
    else:
        return content

with open("test.xml","r") as f:
    print(getdict(f.read().replace('\n','')))

샘플 입력

<details class="4b" count=1 boy>
    <name type="firstname">John</name>
    <age>13</age>
    <hobby>Coin collection</hobby>
    <hobby>Stamp collection</hobby>
    <address>
        <country>USA</country>
        <state>CA</state>
    </address>
</details>
<details empty="True"/>
<details/>
<details class="4a" count=2 girl>
    <name type="firstname">Samantha</name>
    <age>13</age>
    <hobby>Fishing</hobby>
    <hobby>Chess</hobby>
    <address current="no">
        <country>Australia</country>
        <state>NSW</state>
    </address>
</details>

산출량 (미관)

[
  {
    "details": [
      {
        "@attributes": [
          {
            "class": "4b"
          },
          {
            "count": "1"
          },
          {
            "boy": ""
          }
        ]
      },
      {
        "$values": [
          {
            "name": [
              {
                "@attributes": [
                  {
                    "type": "firstname"
                  }
                ]
              },
              {
                "$values": "John"
              }
            ]
          },
          {
            "age": [
              {
                "@attributes": []
              },
              {
                "$values": "13"
              }
            ]
          },
          {
            "hobby": [
              {
                "@attributes": []
              },
              {
                "$values": "Coin collection"
              }
            ]
          },
          {
            "hobby": [
              {
                "@attributes": []
              },
              {
                "$values": "Stamp collection"
              }
            ]
          },
          {
            "address": [
              {
                "@attributes": []
              },
              {
                "$values": [
                  {
                    "country": [
                      {
                        "@attributes": []
                      },
                      {
                        "$values": "USA"
                      }
                    ]
                  },
                  {
                    "state": [
                      {
                        "@attributes": []
                      },
                      {
                        "$values": "CA"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "details": [
      {
        "@attributes": [
          {
            "empty": "True"
          }
        ]
      },
      {
        "$values": ""
      }
    ]
  },
  {
    "details": [
      {
        "@attributes": []
      },
      {
        "$values": ""
      }
    ]
  },
  {
    "details": [
      {
        "@attributes": [
          {
            "class": "4a"
          },
          {
            "count": "2"
          },
          {
            "girl": ""
          }
        ]
      },
      {
        "$values": [
          {
            "name": [
              {
                "@attributes": [
                  {
                    "type": "firstname"
                  }
                ]
              },
              {
                "$values": "Samantha"
              }
            ]
          },
          {
            "age": [
              {
                "@attributes": []
              },
              {
                "$values": "13"
              }
            ]
          },
          {
            "hobby": [
              {
                "@attributes": []
              },
              {
                "$values": "Fishing"
              }
            ]
          },
          {
            "hobby": [
              {
                "@attributes": []
              },
              {
                "$values": "Chess"
              }
            ]
          },
          {
            "address": [
              {
                "@attributes": [
                  {
                    "current": "no"
                  }
                ]
              },
              {
                "$values": [
                  {
                    "country": [
                      {
                        "@attributes": []
                      },
                      {
                        "$values": "Australia"
                      }
                    ]
                  },
                  {
                    "state": [
                      {
                        "@attributes": []
                      },
                      {
                        "$values": "NSW"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

소스가 xml 파일인 경우 다음과 같이 입력합니다.

<pa:Process xmlns:pa="http://sssss">
        <pa:firsttag>SAMPLE</pa:firsttag>
    </pa:Process>

다음 코드를 사용해 보십시오.

from lxml import etree, objectify
metadata = 'C:\\Users\\PROCS.xml' # this is sample xml file the contents are shown above
parser = etree.XMLParser(remove_blank_text=True) # this line removes the  name space from the xml in this sample the name space is --> http://sssss
tree = etree.parse(metadata, parser) # this line parses the xml file which is PROCS.xml
root = tree.getroot() # we get the root of xml which is process and iterate using a for loop
for elem in root.getiterator():
    if not hasattr(elem.tag, 'find'): continue  # (1)
    i = elem.tag.find('}')
    if i >= 0:
        elem.tag = elem.tag[i+1:]

dict={}  # a python dictionary is declared
for elem in tree.iter(): #iterating through the xml tree using a for loop
    if elem.tag =="firsttag": # if the tag name matches the name that is equated then the text in the tag is stored into the dictionary
        dict["FIRST_TAG"]=str(elem.text)
        print(dict)

출력은 다음과 같습니다.

{'FIRST_TAG': 'SAMPLE'}

언급URL : https://stackoverflow.com/questions/1912434/how-to-parse-xml-and-get-instances-of-a-particular-node-attribute

반응형