Así a ojo y usando Amara (http://xml3k.org/Amara)
from amara.bindery import parse doc = parse('ruta al primer doc.xml') nodo = parse('ruta al segundo documento.xml') doc.include.context.xml_insert(0, nodo.extension) doc.xml_write() # muestra en pantalla resultado nodo_eliminar = doc.xml_select(u'//extension[@name="Juanito"]')[0] doc.include.context.xml_remove(nodo_eliminar) Amara (usando el parser de bindery) tiene la ventaja de que siempre usas objetos python y al estilo python y no te exige conocimientos elevados en tecnologías xml. Saludos, -- luismiguel (@lmorillas) El día 17 de marzo de 2014, 17:40, FERNANDO VILLARROEL <fvillarr...@yahoo.com> escribió: > Hola comunidad. > > Les escribo ya que necesito ayuda con un archivo XML, el asunto es que tengo > un XML con la siguiente estructura: > > <?xml version="1.0" encoding="utf-8"?> > > <include> > <context name="default"> > > <extension name="Local_Extension"> > <condition field="destination_number" expression="^(5644890\d{4})$"> > <action application="export" data="dialed_extension=$1"/> > <action application="set" data="dialed_extension=$1"/> > <action application="set" data="call_timeout=30"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_ext}@${domain} var > callgroup)}"/> > <action application="hash" > data="insert/last_dial/${called_party_callgroup}/${uuid}"/> > <action application="set" data="hangup_after_bridge=true"/> > <action application="set" data="continue_on_fail=true"/> > <action application="hash" > data="insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_extension}@${domain_name} > var callgroup)}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/global/${uuid}"/> > <action application="set" data="niblle_account=${nibble_account}"/> > <action application="set" data="nibble_rate=0"/> > <action application="set" data="lcr_rate=0"/> > <action application="bridge" > data="${sofia_contact(${dialed_extension})}"/> > <action application="answer"/> > <action application="sleep" data="1000"/> > <action application="bridge" data="loopback/app=voicemail:default > ${domain_name} ${dialed_extension}"/> > </condition> > </extension> > > </context> > </include> > > Y lo que busco hacer es lo siguiente, debo insertar un pedazo de codigo para > el atributo <extensions> con lo siguiente: > > <extension name="Juanito"> > <condition field="destination_number" expression="^(56448908901)$"> > <action application="export" data="dialed_extension=$1"/> > <action application="set" data="dialed_extension=$1"/> > <action application="set" data="call_timeout=30"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_ext}@${domain} var > callgroup)}"/> > <action application="hash" > data="insert/last_dial/${called_party_callgroup}/${uuid}"/> > <action application="set" data="hangup_after_bridge=true"/> > <action application="set" data="continue_on_fail=true"/> > <action application="hash" > data="insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_extension}@${domain_name} > var callgroup)}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/global/${uuid}"/> > <action application="set" data="niblle_account=${nibble_account}"/> > <action application="set" data="nibble_rate=0"/> > <action application="set" data="lcr_rate=0"/> > <action application="bridge" > data="${sofia_contact(${dialed_extension})}"/> > <action application="answer"/> > <action application="sleep" data="1000"/> > <action application="bridge" data="loopback/app=voicemail:default > ${domain_name} ${dialed_extension}"/> > </condition> > </extension> > > Este atributo debe quedar antes del atributo extensions ya existente de tal > forma que el archivo xml quede de la siguiente forma: > > <?xml version="1.0" encoding="utf-8"?> > > <include> > <context name="default"> > > <extension name="Juanito"> > <condition field="destination_number" expression="^(56448908901)$"> > <action application="export" data="dialed_extension=$1"/> > <action application="set" data="dialed_extension=$1"/> > <action application="set" data="call_timeout=30"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_ext}@${domain} var > callgroup)}"/> > <action application="hash" > data="insert/last_dial/${called_party_callgroup}/${uuid}"/> > <action application="set" data="hangup_after_bridge=true"/> > <action application="set" data="continue_on_fail=true"/> > <action application="hash" > data="insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_extension}@${domain_name} > var callgroup)}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/global/${uuid}"/> > <action application="set" data="niblle_account=${nibble_account}"/> > <action application="set" data="nibble_rate=0"/> > <action application="set" data="lcr_rate=0"/> > <action application="bridge" > data="${sofia_contact(${dialed_extension})}"/> > <action application="answer"/> > <action application="sleep" data="1000"/> > <action application="bridge" data="loopback/app=voicemail:default > ${domain_name} ${dialed_extension}"/> > </condition> > </extension> > > > <extension name="Local_Extension"> > <condition field="destination_number" expression="^(5644890\d{4})$"> > <action application="export" data="dialed_extension=$1"/> > <action application="set" data="dialed_extension=$1"/> > <action application="set" data="call_timeout=30"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_ext}@${domain} var > callgroup)}"/> > <action application="hash" > data="insert/last_dial/${called_party_callgroup}/${uuid}"/> > <action application="set" data="hangup_after_bridge=true"/> > <action application="set" data="continue_on_fail=true"/> > <action application="hash" > data="insert/${domain_name}-call_return/${dialed_extension}/${caller_id_number}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${dialed_extension}/${uuid}"/> > <action application="set" > data="called_party_callgroup=${user_data(${dialed_extension}@${domain_name} > var callgroup)}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/${called_party_callgroup}/${uuid}"/> > <action application="hash" > data="insert/${domain_name}-last_dial_ext/global/${uuid}"/> > <action application="set" data="niblle_account=${nibble_account}"/> > <action application="set" data="nibble_rate=0"/> > <action application="set" data="lcr_rate=0"/> > <action application="bridge" > data="${sofia_contact(${dialed_extension})}"/> > <action application="answer"/> > <action application="sleep" data="1000"/> > <action application="bridge" data="loopback/app=voicemail:default > ${domain_name} ${dialed_extension}"/> > </condition> > </extension> > > </context> > </include> > > De igual manera como lo debo hacer si deseo en algún momento eliminar una > sección extension por ejemplo si quisiera ahora eliminar toda la sección del > atributo <extension name="Juanito"> > > He estado mirando la librería ElementTree, pero veo que hay varias librerías > disponibles, cual me recomiendan. > > Les agradeceré alguna librería que me sirva para este objetivo y o algún > ejemplo descriptivo sobre como insertar y eliminar secciones o atributos. > > Saludos > > _______________________________________________ > Python-es mailing list > Python-es@python.org > https://mail.python.org/mailman/listinfo/python-es > FAQ: http://python-es-faq.wikidot.com/ > _______________________________________________ Python-es mailing list Python-es@python.org https://mail.python.org/mailman/listinfo/python-es FAQ: http://python-es-faq.wikidot.com/