0

I am trying to import XML file with products and categories to Magento.

The problem is, that the attributes like Category, Colour, Size etc. are defined as codes and the code specification is at the end of XML file as < codebook >.

I am looking for solution with Python how to replace codes with attribute names from < codebook >

here is an example:

original XML:

<pricelist>
<item>
  <category>
    <item code="1250" l1="0010" />
    <item code="0610" l1="0010" />
  </category>
 </item>
</pricelist>

<codebook>
 <category>
  <item code="0010" parent="">Catalogue 2015</item>
  <item code="0600" parent="0010">Office</item>
  <item code="1200" parent="0010">Time and temperature measuring</item>
  <item code="1210" parent="1200">Watches and watch sets</item>
  <item code="1250" parent="1200">desktop watches, alarms</item>
  <item code="0610" parent="0600">office table stuff</item>
 </category>
</codebook>
</body>
</pricelist>

desired output XML:

   <pricelist>
    <item>
      <category>
        <item>Catalogue 2015 | Time and temperature measuring | desktop watches, alarms</item>
        <item>Catalogue 2015 | Office | office table stuff</item>
      </category>
    </item>
   </pricelist>

I would appreciate any help, thank you.

1
  • I think the question was too complicated and not clear so I have simplified everything. Commented Oct 21, 2014 at 10:14

2 Answers 2

1

For this kind of tasks Python is the option. I have been using BeautifulSoup for months and it is a breeze to process XML with it.

It also has a nice and easy documentation which will help you out process any kind of XML that you want.

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

1 Comment

thank you, I am just beginning with Python so I would like to make it better with Python's ElementTree.
0

I have now this working code, but as I said before, I am beginner so it would be great to see and learn how to make it better. I thought, maybe scan first the < codebook > and make some kind of dictionary and then go through pricelist and make all changes ? The xml is very big also the codebook with all specifications is large.

this is the code that I have made and which makes desired result:

import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='DGTip_short.xml')
root = tree.getroot()

    def searchforparentofparent(category):
        global parentofparentname
        for elem in tree.iterfind('codebook/category/item'):
            parentofparent = elem.get('parent')
            codetemp = elem.get('code')
            if category == codetemp:  
                parentofparentname = root.findtext('codebook/category/item[@code="'+ parentofparent +'"]')

    for elem in tree.iterfind('pricelist/item/category/item'):
        parent = elem.get('l1')
        category = elem.get('code')
        parentname = root.findtext('codebook/category/item[@code="'+ parent +'"]')
        categoryname = root.findtext('codebook/category/item[@code="'+ category +'"]')
        searchforparentofparent(category)

        magentoattrib = parentname + " | " + parentofparentname + " | " + categoryname
        elem.text = (magentoattrib)
        del elem.attrib['code']
        del elem.attrib['l1']

    tree.write('PriceList2.xml', "UTF-8")

also I couldn't get to work loop inside of loop so that is why I made the function which worked.

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.