Python reading xml

2024/7/7 6:15:07

I am newbie on Python programming. I have requirement where I need to read the xml structure and build the new soap request xml by adding namespace like here is the example what I have

Below XML which i get from other system:

<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>

I want final result like below

<?xml version="1.0"?>
<soa:foo xmlns:soa="https://www.w3schools.com/furniture"><soa:bar><soa:type foobar="1"/><soa:type foobar="2"/></soa:bar>
</soa:foo>

I tried to look in python document but not able to find

Answer

One option is to use lxml to iterate over all of the elements and add the namespace uri to the .tag property.

You can use register_namespace() to bind the uri to the desired prefix.

Example...

from lxml import etreetree = etree.parse("input.xml")etree.register_namespace("soa", "https://www.w3schools.com/furniture")for elem in tree.iter():elem.tag = f"{{https://www.w3schools.com/furniture}}{elem.tag}"print(etree.tostring(tree, pretty_print=True).decode())

Printed output...

<soa:foo xmlns:soa="https://www.w3schools.com/furniture"><soa:bar><soa:type foobar="1"/><soa:type foobar="2"/></soa:bar>
</soa:foo>
https://en.xdnf.cn/q/120059.html

Related Q&A

How can sum two nested list in this situation

Given list a, b a=[[[1.1,-2.1],[-0.6,4.2]],[[3.9,1.3],[-1.3,1.2]]]b=[[-1.1,4.3],[-1.4,2.4]]If I just want to sum the list [[1.1,-2.1],[-0.6,4.2]] in the list a (not the whole list a) with the list [-1.…

Create check digit function

Im trying to create check digits and append them after the original UPCs. Heres the sample data Because there are leading 0s, I have to read the data as strings first: import pandas as pd …

Getting count of permutations in a faster way

Using this code to get count of permutations is slow on big numbers as the partition part takes long time to calculate all the partitions for a number like 100 and because of all the partitions in the …

How to find number of vowels in each word of string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python error: AttributeError: str object has no attribute read

My full code:import requests as req import json Bin = int(300000) BinMax = int(600000) File = open("C:/Users/admin/Desktop/PS Now Generaetors/Bins.txt", a)while bin != BinMax:json1 = req.get(…

list elements sum of one list first element to last element of second list

How to sum first element of one list to last element of second list if both list contains same amount of elements in python. Source code (should be kind of like this): def update(l1,l2,l3):for i in ran…

Append float data at the end of each line in a text file

I have a .txt file and Im trying to add float number at the end of each line with incremented value than the last line. This is what I have in the .txt file:>520.980000 172.900000 357.440000 >320…

Change and Sort list in list in specific order in python

Hello guys I have this type of data in list in list array = [ ["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AA…

How to interact with the reCAPTCHA Solve the challenge button using Selenium and Python

Im trying to interact with the recaptcha Solve the challenge button on image verification popup using Selenium and Python. The xpath looks correct in dev tools but using Selenium unable to interact wit…

How to convert an adjacency matrix to an adjacency list with python?

I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.],[ 15., 0., 9., 11., 0., 9.],[ 0., 9., 0., 0., 12., 7.],[ 7., 11., 0., 0., 8., 14.],[ 10., 0., 12., …