0

I have XML where I have to add a child node to the parent node using python. I have the XML

<?xml version="1.0" ?><PLMRequest><StyleNumber>20SM1401</StyleNumber><StyleName>PG001 Style</StyleName><Theme>Ocean</Theme><ProductSpecialist>Ashok Gangisetty</ProductSpecialist><Color><ColorCode>0816</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0044</ColorCode><UserDefinedField1>INF</UserDefinedField1><UserDefinedField2>TH02</UserDefinedField2></Color><Color><ColorCode>0461</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0430</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0646</ColorCode><UserDefinedField1>INN</UserDefinedField1><UserDefinedField2>TH02</UserDefinedField2></Color><Color><ColorCode>C801</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>C750</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0110</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>SK</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>KS</ColorCode><UserDefinedField1/><UserDefinedField1/></Color></PLMRequest>

I want output like

<?xml version="1.0" ?><PLMRequest><Overview><StyleNumber>20SM1401</StyleNumber><StyleName>PG001 Style</StyleName><Theme>Ocean</Theme><ProductSpecialist>Ashok Gangisetty</ProductSpecialist></Overview><ColorInfo><Color><ColorCode>0816</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0044</ColorCode><UserDefinedField1>INF</UserDefinedField1><UserDefinedField2>TH02</UserDefinedField2></Color><Color><ColorCode>0461</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0430</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0646</ColorCode><UserDefinedField1>INN</UserDefinedField1><UserDefinedField2>TH02</UserDefinedField2></Color><Color><ColorCode>C801</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>C750</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>0110</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>SK</ColorCode><UserDefinedField1/><UserDefinedField1/></Color><Color><ColorCode>KS</ColorCode><UserDefinedField1/><UserDefinedField1/></Color></ColorInfo></PLMRequest>

Python code i have used to convert Json to XML

import json
from xml.etree import ElementTree
from xml.etree import cElementTree
import ast
import xml.etree.cElementTree as ET
import xmltodict
import xml.dom.minidom
import xml.etree.ElementTree as DT
class Test(object):
    def __init__(self, data):
        self.__dict__ = json.loads(data)
styleobj =Test(APIsInputData)
styleDto = styleobj.value[0]
root = ET.Element('PLMRequest')
ET.SubElement(root,"StyleNumber").text = styleDto["StyleCode"]
ET.SubElement(root,"StyleName").text = styleDto["Name"]
ET.SubElement(root,"Theme").text = styleDto["Theme"]["Name"]
ET.SubElement(root,"ProductSpecialist").text = styleDto["ProdSpecialist"]["Name"]
for color in styleDto["StyleColorways"]:
    StyleColorways = ET.SubElement(root,"Color")
    ET.SubElement(StyleColorways,"ColorCode").text = color["Code"]
    if color["UserDefinedOne"] is None:
        ET.SubElement(StyleColorways,"UserDefinedField1").text = color["UserDefinedOne"]
    else:
        ET.SubElement(StyleColorways,"UserDefinedField1").text = color["UserDefinedOne"]["Code"]
    if color["UserDefinedTwo"] is None:
        ET.SubElement(StyleColorways,"UserDefinedField1").text = color["UserDefinedOne"]
    else:
        ET.SubElement(StyleColorways,"UserDefinedField2").text = color["UserDefinedTwo"]["Code"]
tree_out = ET.tostring(root, encoding="UTF-8")
newXML = xml.dom.minidom.parseString(tree_out.decode('UTF-8'))
pretty_xml = newXML.toprettyxml()
Outputvalue = pretty_xml

Json file I used

{"@odata.context":"https://prd-euc1.fplm.eu1.inforcloudsuite.com/api/odata/$metadata#STYLE","value":[{"@odata.etag":"W/\"YmluYXJ5J0FBQUFBQUg0RzFNPSc=\"","StyleId":1238,"StyleCode":"20SM1401","Name":"PG001 Style","IsDeleted":0,"StyleColorways":[{"StyleColorwayId":766,"StyleId":1238,"Code":"0816","Name":"Saffron","Description":"Saffron"},{"StyleColorwayId":767,"StyleId":1238,"Code":"0044","Name":"Swan White","Description":"Swan White","UserDefinedOne":{"Id":1,"Code":"INF","Name":"In For","ModifyId":1,"ModifyDate":"2018-07-16T08:54:12.803Z"},"UserDefinedTwo":{"Id":2,"Code":"TH02","Name":"Theme 02","ModifyId":1,"ModifyDate":"2018-07-16T08:55:59.073Z"}},{"StyleColorwayId":768,"StyleId":1238,"Code":"0461","Name":"Pastel Blue","Description":"Pastel Blue"},{"StyleColorwayId":769,"StyleId":1238,"Code":"0430","Name":"Sport Blue","Description":"Sport Blue"},{"StyleColorwayId":770,"StyleId":1238,"Code":"0646","Name":"Light Rose Water ","Description":"Light Rose Water ","UserDefinedOne":{"Id":2,"Code":"INN","Name":"In Next","ModifyId":1,"ModifyDate":"2018-07-16T08:54:39.96Z"},"UserDefinedTwo":{"Id":2,"Code":"TH02","Name":"Theme 02","ModifyId":1,"ModifyDate":"2018-07-16T08:55:59.073Z"}},{"StyleColorwayId":771,"StyleId":1238,"Code":"C801","Name":"Shocking Orange / Navy Blue","Description":"High Visibility Shocking Orange / Navy Blue"},{"StyleColorwayId":772,"StyleId":1238,"Code":"C750","Name":"Flame Red / Black","Description":"High Visibility Flame Red / Black"},{"StyleColorwayId":773,"StyleId":1238,"Code":"0110","Name":"Silver","Description":""},{"StyleColorwayId":781,"StyleId":1238,"Code":"SK","Name":"SK01","Description":""},{"StyleColorwayId":782,"StyleId":1238,"Code":"KS","Name":"KS01","Description":""}],"Theme":{"Id":10,"Code":"OCEAN","Name":"Ocean","ModifyId":1,"ModifyDate":"2020-02-11T10:53:32.333Z"},"ProdSpecialist":{"UserId":34,"UserGuid":"5c302bc2-616e-4943-9baf-323feecfd65c","UserName":"[email protected]","Name":"Ashok Gangisetty","IsActivated":false}}]}
7
  • Please figure out how to make a minimal example. XML you provided is too long. Commented Apr 4, 2023 at 10:44
  • 1
    Where's your Python code? And which XML library are you using? Commented Apr 4, 2023 at 10:46
  • Please format your code properly. Nobody's going to read that. And add a significantly smaller JSON as your example. Commented Apr 4, 2023 at 15:43
  • I have changed my Code and Jaosn Commented Apr 5, 2023 at 5:19
  • Does this answer your question? How to add nested child nodes to a parent node in xml documents using python? Commented Apr 6, 2023 at 20:19

0

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.