繁体   English   中英

适用于python的理想xml解析器

[英]Ideal xml parser for python

我一直在尝试使用lxml读取xml文件并替换类别标签和子类别标签之间的值。

我想将新的xml重定向到一个新文件。

    data = """<xml>
<questionset author="Joee Foo" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.escreeningz.com"
xsi:schemaLocation="http://www.escreeningz.com ../xsd/QuestionSet.xsd">
<question    status="A" identifier="SampleQuestions.xml_1">
    <classification>
        <type>CONCEPTUAL</type>
        <category>Core Java</category>
        <subcategory>Exception Handling</subcategory>
        <difficulty>2</difficulty>
    </classification>
    <tags>
        <tag>Exception Hierarchy</tag>
        <tag>Checked and Unchecked exceptions</tag>
        <tag>Finally Block</tag>
    </tags>
    <preface>
        <section order="1" type="STANDARD">
            <value><![CDATA[Which of the statements regarding exceptions in Java are true?]]></value>
        </section>
    </preface>
    <answers>
        <answer correct="false" score="-4">
            <value><![CDATA[Checked exceptions extend java.lang.RuntimeException.
            ]]></value>
            <explain>It is uncheked exception that extend java.lang.Runtime;not checked</explain>
        </answer>
        <answer correct="true" score="5">
            <value><![CDATA[The base class for all Exceptions and Errors is java.lang.Throwable.
            ]]></value>
        </answer>
        <answer correct="true" score="5">
            <value><![CDATA[Any method that might throw a checked exception like java.io.IOException must either declare 
  the exception using the throws keyword or handle the exception with an appropriate try/catch.
  ]]></value>
        </answer>
        <answer correct="true" score="2">
            <value><![CDATA[If you use a finally block, it will always be invoked regardless of whether an exception in the corresponding try is thrown or not 
  and regardless of whether a thrown exception is caught or not as long as the JVM is running.
  ]]></value>
        </answer>
        <answer correct="false" score="-4">
            <value><![CDATA[All catch blocks must be ordered as general caught first to specific caught last]]></value>
            <explain>All catch blocks must be ordered from specific to general</explain>
        </answer>   
    </answers>
</question>
</questionset>
</xml>
"""

# csv to xml conversion

import sys
import os
import csv
import xml.etree.ElementTree as ET
from lxml import etree
from StringIO import StringIO
from lxml.etree import Element
stream = StringIO(data)
context = etree.iterparse(stream, events=("start", ))

现在,当我尝试从和标记之间提取值时,它将返回一个空列表。 :(

for action,elem in context:
    for child in elem.findall('{http://www.escreeningz.com}category'):
        print child.attrib

Result is : {}

当我运行以下代码时,它会正确返回标记:

for action,elem in context:
    for child in elem.findall('{http://www.escreeningz.com}category'):
        print child.tag

Result: {http://www.escreeningz.com}category

我在这里想念什么吗?

我终于要更换

<category>Core Java</category>

 <category>SQL</category>

这个stackoverflow问题对xml解析有足够的讨论

暂无
暂无

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

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