Note that there are some explanatory texts on larger screens.

plurals
  1. POXML to MYSQL Using Python
    primarykey
    data
    text
    <p>I have the folowing test.xml</p> <pre><code>&lt;root&gt; &lt;parent&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;child1&gt;Value1&lt;/child1&gt; &lt;child2&gt;value11&lt;/child2&gt; &lt;child3&gt; &lt;subchild&gt;value111&lt;/subchild&gt; &lt;/child3&gt; &lt;/parent&gt; &lt;parent&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;child1&gt;value2&lt;/child1&gt; &lt;child2&gt;value22&lt;/child2&gt; &lt;child2&gt;value333&lt;/child2&gt; &lt;/parent&gt; &lt;parent&gt; &lt;ID&gt;3&lt;/ID&gt; &lt;child1&gt;value3&lt;/child1&gt; &lt;child2&gt;value33&lt;/child2&gt; &lt;/parent&gt; &lt;parent&gt; &lt;ID&gt;4&lt;/ID&gt; &lt;child1&gt;value4&lt;/child1&gt; &lt;child2&gt;value44&lt;/child2&gt; &lt;/parent&gt; &lt;/root&gt; </code></pre> <p>What Im trying to accomplish is the following: I want to iterate through the test.xml and for every parent I want to put all of the child nodes in a dictionary where the tag is the index and the text is the value and once i get to the end of the parent add that to the database and reset the dictionary and move onto the next parent.</p> <p>So for the first parent I would want</p> <pre><code> insert = {'ID':1,'child1':'value1','child2':'value11','subchild':'value111'} </code></pre> <p>Use it in an SQL query, And then move onto the next parent reset the dictionary and do the same thing. Not every parent has the same amount of children, and some children have sub children. </p> <p>I have tried with:</p> <pre><code> value = [] tag = [] from elementtree import ElementTree as ET for parent in tree.getiterator(): for child in parent: value.append(child.text) tag.append(child.tag) </code></pre> <p>But I couldn't figure out how to get my desired results. I left out retrieving and opening the xml in order to keep the post as simple as possible. This is the method I was attempting to use but I don't think its the right one because I haven't been able to stop the iteration at the end of the parent tag in order to insert.</p> <p>Any help would be greatly appreciated! thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload