Add Text to an XML element in python3

Viewed 50

I have a XML-file:

<listOfSpecies>
  <species metaid="MAM00001c" sboTerm="SBO:0000247" id="MAM00001c" name="(-)-trans-carveol" compartment="c" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false" fbc:charge="0" fbc:chemicalFormula="C10H16O">
    <annotation>
      <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" xmlns:vCard4="http://www.w3.org/2006/vcard/ns#" xmlns:bqbiol="http://biomodels.net/biology-qualifiers/" xmlns:bqmodel="http://biomodels.net/model-qualifiers/">
        <rdf:Description rdf:about="#MAM00001c">
          <bqbiol:is>
            <rdf:Bag>
              
            </rdf:Bag>
          </bqbiol:is>
        </rdf:Description>
      </rdf:RDF>
    </annotation>
  </species>
 ...
</listOfSpecies>

and a txt-file:

name="(-)-trans-carveol" fbc:charge="0" fbc:chemicalFormula="C10H16O"
<rdf:li rdf:resource="https://identifiers.org/kegg.compound/C11409"/>
<rdf:li rdf:resource="https://identifiers.org/pubchem.compound/94221"/>
<rdf:li rdf:resource="https://identifiers.org/lipidmaps/LMPR0102090005"/>
<rdf:li rdf:resource="https://identifiers.org/inchi/InChI=1S/C10H16O/c1-7(2)9-5-4-   8(3)10(11)6-9/h4,9-11H,1,5-6H2,2-3H3/t9-,10+/m0/s1"/>
<rdf:li rdf:resource="https://identifiers.org/inchikey/BAVONGHXFVOKBV-VHSXEESVSA-N"/>
<rdf:li rdf:resource="https://identifiers.org/metanetx.chemical/MNXM45735"/>

and I would like to insert all the 'rdf:li rdf:resource' elements in the txt-file for each species/name between the rdf:Bag</rdf:Bag>tags in the xml-file. So far I have been playing with minidom, beautifulsoup, elementree and treating the xml-file as a regular file and I have not found anything that works so far. Can anyone point me in the right direction?

1 Answers

To add all rdf:li rdf:resource elements from the txt file to XML file under the <rdf:Bag> tags, you can loop over both files using zip() and use Tag.insert() to add a new tag.

Here's an example, you'll have to modify it a bit to read the tags from a file instead of a docstring:

from bs4 import BeautifulSoup


xml = """
<listOfSpecies>
  <species metaid="MAM00001c" sboTerm="SBO:0000247" id="MAM00001c" name="(-)-trans-carveol" compartment="c" initialConcentration="0" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false" fbc:charge="0" fbc:chemicalFormula="C10H16O">
    <annotation>
      <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" xmlns:vCard4="http://www.w3.org/2006/vcard/ns#" xmlns:bqbiol="http://biomodels.net/biology-qualifiers/" xmlns:bqmodel="http://biomodels.net/model-qualifiers/">
        <rdf:Description rdf:about="#MAM00001c">
          <bqbiol:is>
            <rdf:Bag>
              
            </rdf:Bag>
          </bqbiol:is>
        </rdf:Description>
      </rdf:RDF>
      <rdf:Bag>
              
            </rdf:Bag>
    </annotation>
  </species>
</listOfSpecies>
"""

txt = """
name="(-)-trans-carveol" fbc:charge="0" fbc:chemicalFormula="C10H16O"
<rdf:li rdf:resource="https://identifiers.org/kegg.compound/C11409"/>
<rdf:li rdf:resource="https://identifiers.org/pubchem.compound/94221"/>
<rdf:li rdf:resource="https://identifiers.org/lipidmaps/LMPR0102090005"/>
<rdf:li rdf:resource="https://identifiers.org/inchi/InChI=1S/C10H16O/c1-7(2)9-5-4-   8(3)10(11)6-9/h4,9-11H,1,5-6H2,2-3H3/t9-,10+/m0/s1"/>
<rdf:li rdf:resource="https://identifiers.org/inchikey/BAVONGHXFVOKBV-VHSXEESVSA-N"/>
<rdf:li rdf:resource="https://identifiers.org/metanetx.chemical/MNXM45735"/>
"""

xml_soup = BeautifulSoup(xml, "lxml")
txt_soup = BeautifulSoup(txt, "lxml")

for resource, bag in zip(txt_soup.find_all("rdf:li"), xml_soup.find_all("rdf:bag")):
    bag.insert(0, resource["rdf:resource"])

print(xml_soup.prettify())

Output:

<listofspecies>
 <species boundarycondition="false" compartment="c" constant="false" fbc:charge="0" fbc:chemicalformula="C10H16O" hasonlysubstanceunits="false" id="MAM00001c" initialconcentration="0" metaid="MAM00001c" name="(-)-trans-carveol" sboterm="SBO:0000247">
  <annotation>
   <rdf:rdf xmlns:bqbiol="http://biomodels.net/biology-qualifiers/" xmlns:bqmodel="http://biomodels.net/model-qualifiers/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" xmlns:vcard4="http://www.w3.org/2006/vcard/ns#">
    <rdf:description rdf:about="#MAM00001c">
     <bqbiol:is>
      <rdf:bag>
       https://identifiers.org/kegg.compound/C11409
      </rdf:bag>
     </bqbiol:is>
    </rdf:description>
   </rdf:rdf>
   <rdf:bag>
    https://identifiers.org/pubchem.compound/94221
   </rdf:bag>
  </annotation>
 </species>
</listofspecies>
Related