Hi I have following XML file. I am trying to change the text file in the ID tag . <ID>xxx</ID>
But no matter what I try it refuses to change or give me back the value in . I am a noob
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd="w3.org/2001/XMLSchema"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns="asml.com/XMLSchema/XXX/v1.0">
<ID>Some String</ID>
<AList>
<Attribute>
<Name>SomeName</Name>
<Value>SomeValue</Value>
</Attribute>
<Attribute>
<Name>SomeName_2</Name>
<Value>SomeValue_2</Value>
</Attribute>
<Attribute>
<Name>SomeName_3</Name>
<Value>SomeValue_3</Value>
</Attribute>
</AList>
<AGroupList>
<Group>
<GroupKeyList>
<Attribute>
<Name>SomeName_4</Name>
<Value>SomeValue_4</Value>
</Attribute>
</GroupKeyList>
<GroupAList>
<Attribute>
<Name>SomeName_5</Name>
<Value>SomeValue_5</Value>
</Attribute>
</GroupAList>
</Group>
<Group>
<GroupKeyList>
<Attribute>
<Name>SomeName_6</Name>
<Value>SomeValue_6</Value>
</Attribute>
</GroupKeyList>
<GroupAList>
<Attribute>
<Name>SomeName_7</Name>
<Value>SomeValue_7</Value>
</Attribute>
</GroupAList>
</Group>
</AGroupList>
from lxml import etree
xml_elem = etree.parse('path/to/file.xml')
id_elem = xml_elem.find('ID')
id_elem.text = 'xxx'
THis is my code, I have tried everything
Just add a </ROOT>
tag at the end of your xml. Then it's as simple as:
from lxml import etree
xml_elem = etree.parse('path/to/file.xml')
id_elem = xml_elem.find('ID')
id_elem.text = 'xxx'
Main issue is the undeclared namespace prefix xmlns="asml.com/XMLSchema/XXX/v1.0"
which unlike the others does not have a colon separated identifier. This requires you to qualify search paths like find
with the URI. Do note: this is completely valid in XML files.
However, your posted markup is not well-formed as you have a duplicate prefix: xmlns:xsd
and have semicolons between namespaces. Do note: this is NOT valid XML. If your actual XML <ROOT>
follows the below well-formed version, the following lxml
code should work to update the ID text:
XML
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd2="w3.org/2001/XMLSchema"
xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="asml.com/XMLSchema/XXX/v1.0">
...
</ROOT>
Python
from lxml import etree
xml_elem = etree.parse('AGroupList.xml')
id_elem = xml_elem.find('{asml.com/XMLSchema/XXX/v1.0}ID') # URI MAPPING
id_elem.text = 'xxx'
print(etree.tostring(xml_elem, pretty_print=True).decode("UTF-8"))
Output
<ROOT xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd2="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="asml.com/XMLSchema/XXX/v1.0">
<ID>xxx</ID>
<AList>
<Attribute>
<Name>SomeName</Name>
<Value>SomeValue</Value>
</Attribute>
<Attribute>
<Name>SomeName_2</Name>
<Value>SomeValue_2</Value>
</Attribute>
<Attribute>
<Name>SomeName_3</Name>
<Value>SomeValue_3</Value>
</Attribute>
</AList>
<AGroupList>
<Group>
<GroupKeyList>
<Attribute>
<Name>SomeName_4</Name>
<Value>SomeValue_4</Value>
</Attribute>
</GroupKeyList>
<GroupAList>
<Attribute>
<Name>SomeName_5</Name>
<Value>SomeValue_5</Value>
</Attribute>
</GroupAList>
</Group>
<Group>
<GroupKeyList>
<Attribute>
<Name>SomeName_6</Name>
<Value>SomeValue_6</Value>
</Attribute>
</GroupKeyList>
<GroupAList>
<Attribute>
<Name>SomeName_7</Name>
<Value>SomeValue_7</Value>
</Attribute>
</GroupAList>
</Group>
</AGroupList>
</ROOT>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.