0

I have been trying to use lxml to read a xml file and replace the values between category and subcategory tags.

I want re-direct the new xml to a new file.

    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", ))

Now, when I try to extract the value from between and tags, it returns an empty list. :(

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

Result is : {}

When I run the below code, it returns the tag correctly:

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

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

Am I missing something here.

I finally want to replace

<category>Core Java</category>

with

 <category>SQL</category>
3
  • 2
    The category element has no attributes, so findall finds the category elements, and there are no attributes to find among them, so it is returning correctly. Commented Aug 11, 2015 at 16:39
  • Is there a way to find the text between the category tags ? I may have to iterate to find text between multiple tags. But, that is the 2nd step i think. Commented Aug 11, 2015 at 16:41
  • 2
    for your example, print child.text. I suggest checking out the lxml documentation and reading about XML structure. Commented Aug 11, 2015 at 16:43

1 Answer 1

0

This stackoverflow questions had enough discussion about xml parsing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.