XML bir kap kaldırma

oy
-1

Bu benim girdi dosyası .Deliberately onlar question.I ilgilendirmeyen düşündüğümüz gibi huge.I sadece iki konteyner eklemiş olduğu gibi tüm dosyayı yapıştırmadıysanız dışarı başlıklarını bıraktı geçerli:

  <ECUC-CONTAINER-VALUE>
     <SHORT-NAME>ABC</SHORT-NAME>
     <DEFINITION-REF DEST=ECUC-PARAM-CONF-CONTAINER-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
     <PARAMETER-VALUES>
       <ECUC-NUMERICAL-PARAM-VALUES>
         <DEFINITION-REF DEST=ECUC-INTEGER-PARAM-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue</DEFINITION-REF>
          <VALUE>1053</VALUE>
       </ECUC-NUMERICAL-PARAM-VALUES>
       <ECUC-TEXTUAL-PARAM-VALUES>
       <DEFINITION-REF DEST=ECUC-ENUMERATION-PARAM-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
           <VALUE>TRUE</VALUE>
        </ECUC-TEXTUAL-PARAM-VALUES>
      </PARAMETER-VALUES>        
<ECUC-CONTAINER-VALUE>

    <ECUC-CONTAINER-VALUE>
     <SHORT-NAME>ABC</SHORT-NAME>
     <DEFINITION-REF DEST=ECUC-PARAM-CONF-CONTAINER-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
     <PARAMETER-VALUES>
       <ECUC-NUMERICAL-PARAM-VALUES>
         <DEFINITION-REF DEST=ECUC-INTEGER-PARAM-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue</DEFINITION-REF>
          <VALUE>1054</VALUE>
       </ECUC-NUMERICAL-PARAM-VALUES>
       <ECUC-TEXTUAL-PARAM-VALUES>
       <DEFINITION-REF DEST=ECUC-ENUMERATION-PARAM-DEF>/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
           <VALUE>FALSE</VALUE>
        </ECUC-TEXTUAL-PARAM-VALUES>
 </PARAMETER-VALUES>        
<ECUC-CONTAINER-VALUE>

Etrafa 100 var <ECUC-CONTAINER-VALUE>benim xml etiketlerini. Ben silmek zorunda <ECUC-NUMERICAL-PARAM-VALUES>metni ise konteyner <DEFINITION-REF DEST=ECUC-INTEGER-PARAM-DEF>konteyner öyle aşağıda /AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue Ama result.Kindly yardım almıyorum.

Ben yazdım komut dosyası:

import xml.etree.ElementTree
tree = ET.parse('a.xml')
root = tree.getroot()

for child in root.findall(.//ECUC-NUMERICAL-PARAM-VALUE):
    for gchild in child.findall(.//DEFINITION-REF [@DEST='ECUC-INTEGER-PARAM-DEF']):
         string = gchild.find(VALUE).text
         if string == /AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue:
             root.remove(child)
Oluştur 27/11/2018 saat 18:18
kaynak kullanıcı
Diğer dillerde...                            


1 cevaplar

oy
1

Eğer kaldırmak istiyorsanız ECUC-NUMERICAL-PARAM-VALUES, size, yani ana seçmeniz gerekir. Yani sizin iterasyon yaparak deneyin PARAMETER-VALUESdüzeyde.

Örnek...

XML giriş ( a.xml; güncellenmiş bir şekilde biçimlendirilmiş olması için)

<doc>
    <ECUC-CONTAINER-VALUE>
        <SHORT-NAME>ABC</SHORT-NAME>
        <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
        <PARAMETER-VALUES>
            <ECUC-NUMERICAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue</DEFINITION-REF>
                <VALUE>1053</VALUE>
            </ECUC-NUMERICAL-PARAM-VALUES>
            <ECUC-TEXTUAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
                <VALUE>TRUE</VALUE>
            </ECUC-TEXTUAL-PARAM-VALUES>
        </PARAMETER-VALUES>
    </ECUC-CONTAINER-VALUE>
    <ECUC-CONTAINER-VALUE>
        <SHORT-NAME>ABC</SHORT-NAME>
        <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
        <PARAMETER-VALUES>
            <ECUC-NUMERICAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue</DEFINITION-REF>
                <VALUE>1054</VALUE>
            </ECUC-NUMERICAL-PARAM-VALUES>
            <ECUC-TEXTUAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
                <VALUE>FALSE</VALUE>
            </ECUC-TEXTUAL-PARAM-VALUES>
        </PARAMETER-VALUES>
    </ECUC-CONTAINER-VALUE>
</doc>

piton

import xml.etree.ElementTree as ET

tree = ET.parse('a.xml')

for p_vals in tree.findall(".//PARAMETER-VALUES"):
    for num_p_vals in p_vals.findall(".//ECUC-NUMERICAL-PARAM-VALUES"):
        def_ref = num_p_vals.find("DEFINITION-REF[@DEST='ECUC-INTEGER-PARAM-DEF']")
        if def_ref is not None and def_ref.text == \
                "/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue":
            p_vals.remove(num_p_vals)

ET.dump(tree)

Çıktı

<doc>
    <ECUC-CONTAINER-VALUE>
        <SHORT-NAME>ABC</SHORT-NAME>
        <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
        <PARAMETER-VALUES>
            <ECUC-TEXTUAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
                <VALUE>TRUE</VALUE>
            </ECUC-TEXTUAL-PARAM-VALUES>
        </PARAMETER-VALUES>
    </ECUC-CONTAINER-VALUE>
    <ECUC-CONTAINER-VALUE>
        <SHORT-NAME>ABC</SHORT-NAME>
        <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject</DEFINITION-REF>
        <PARAMETER-VALUES>
            <ECUC-TEXTUAL-PARAM-VALUES>
                <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANHandleType</DEFINITION-REF>
                <VALUE>FALSE</VALUE>
            </ECUC-TEXTUAL-PARAM-VALUES>
        </PARAMETER-VALUES>
    </ECUC-CONTAINER-VALUE>
</doc>

Eğer LXML kullanmak mümkün iseniz, bu elementtree çok daha iyi XPath desteği vardır. Ayrıca ebeveyn eleman erişebilir konum getparent(). Bence bu cevabı kolaylaştırır.

Örnek ... (aynı giriş yukarıdaki yukarıdaki ile aynı çıkış üretir)

from lxml import etree

tree = etree.parse('a.xml')

for num_p_vals in tree.xpath(".//ECUC-NUMERICAL-PARAM-VALUES[DEFINITION-REF[@DEST='ECUC-INTEGER-PARAM-DEF']='/AUTOSAR_CAN/EcucModuleDefs/CanConfigSet/CanHardwareObject/CANIdValue']"):
    num_p_vals.getparent().remove(num_p_vals)

etree.dump(tree.getroot())
Cevap 27/11/2018 saat 20:31
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more