1

I exported a mysql database to xml with phpmyadmin and now I would like to parse it with minidom but I'm having trouble getting the content in the form that I need it.

Summary: I need to assign the variable title to the text contained within <column name="news_title">This is the title</column>

The extracted db looks like this:

<pma_xml_export version="1.0" >
    <database name="dbname">
        <!-- Table newsbox -->
        <table name="newsbox">
            <column name="news_id">1</column>
            <column name="news_title">This is the title</column>
            <column name="news_text">This is the news text</column>
            <column name="date">Thu, 28 Feb 2008 20:10:30 -0500</column>
            <column name="author">author</column>
            <column name="category">site_announcement</column>
        </table>
    </database>
</pma_xml_export>

I am able to extract the text with the following script but it's not in the form that I need:

doc = parseString(document)

pmaexport = doc.getElementsByTagName("pma_xml_export")[0]
columns = pmaexport.getElementsByTagName("column")


for item in columns:
    name = item.getAttribute("name")
    text = item.firstChild.data.strip()
    print name, text

What I need is something where I can assign the text contents of these elements to variables which can be passed on e.g.,

for item in columns:
    title = ???
    text = ???
    date = ???
    author = ???

If the db output was in the form of <title>Here's the Title</title> I would have plenty of examples to go off, but I just can't find any reference to something like <column name="news_title">This is the title</column>

1 Answer 1

1

It's been a while since I've used xml.dom.minidom but this should work...

columns = [c.firstChild.data for c in pmaexport.getElementsByTagName('column') if c.getAttribute('name') == 'news_title']

Plus, like, list comprehension!

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.