简体   繁体   English

打印在 xml python 中没有属性的标签行

[英]print lines of tags that have no attribute in xml python

Let's assume that we have xml file:假设我们有 xml 文件:

<School Name = "school1">
  <Class Name = "class A">
    <Student Name = "student"/>
    <Student/>
    <!-- -->
  </Class>
</School>

And I have a python script that using parsing.我有一个使用解析的 python 脚本。 I want to print the line of a tag.我想打印标签行。 For example I want to print lines of tags that have no "Name" attribute.例如,我想打印没有“名称”属性的标签行。 Is it possible?可能吗? I saw an example with inheritance ElementTree but couldn't understand it.我看到了一个继承 ElementTree 的例子,但无法理解。

import xml.etree.ElementTree as ET

def read_root(root):
  for x in root:
    print(x.lineNum)
    read_root(x)

def main():
  fn = "a.xml"
  try:
    tree = ET.parse(fn)
  except ET.ParseError as e:
    print("\nParse error:", str(e))
    print("while reading: " + fn)
    exit(1)
  root = tree.getroot()
  read_root(root)

Your question is so unclear.你的问题太不清楚了。 Anyways, if you just want to check if the tag has a Name attribute and want to print that line number, you can use etree from lxml as shown below:无论如何,如果您只想检查标签是否具有Name属性并想打印该行号,您可以使用etree中的lxml ,如下所示:

from lxml import etree

doc = etree.parse('test.xml')

for element in doc.iter():
    # Check if the tag has a "Name" attribute
    if "Name" not in element.attrib:
        print(f"Line {element.sourceline}: {element.tag}"))

output:输出:

Line 4: Student
Line 5: <cyfunction Comment at 0x13b8e6dc0>

You need a parser like ET.XMLPullParser what can read "comment" and "process instructions" , "namespces" , "start" and "end" events.您需要一个像ET.XMLPullParser这样的解析器,它可以读取"comment""process instructions""namespces""start""end"事件。

If your XML file 'comment.xml' looks like:如果您的 XML 文件'comment.xml'如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<School Name = "school1">
  <Class Name = "class A">
    <Student Name = "student"/>
    <Student/>
    <!-- Comment xml -->
  </Class>
</School>

You can parse to find TAG's without the attribute "Name" and comments:您可以解析以查找没有属性"Name"和注释的TAG's

import xml.etree.ElementTree as ET

#parser = ET.XMLPullParser(['start', 'end', "comment", "pi", "start-ns", "end-ns"])

parser = ET.XMLPullParser([ 'start', 'end', 'comment'])
with open('comment.xml', 'r', encoding='utf-8') as xml:
    feedstring = xml.readlines()
    
for line in enumerate(feedstring):
    parser.feed(line[1])

    for event, elem in parser.read_events():
        if elem.get("Name"):
            pass
        else:
            print(f"{line[0]} Event:{event} | {elem.tag}, {elem.text}")

Output:输出:

4 Event:start | Student, None
4 Event:end | Student, None
5 Event:comment | <function Comment at 0x00000216C4FDA200>,  Comment xml 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM