How to Remove an Xml Element from File

How to remove an xml element from file?

You could try something like this:

string xmlInput = @"<Snippets>
<Snippet name=""abc"">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>

<Snippet name=""def"">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
</Snippets>";

// create the XML, load the contents
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlInput);

// find a node - here the one with name='abc'
XmlNode node = doc.SelectSingleNode("/Snippets/Snippet[@name='abc']");

// if found....
if (node != null)
{
// get its parent node
XmlNode parent = node.ParentNode;

// remove the child node
parent.RemoveChild(node);

// verify the new XML structure
string newXML = doc.OuterXml;

// save to file or whatever....
doc.Save(@"C:\temp\new.xml");
}

Remove an XML element based on child index

In your case looping XmlNodeList not required.

try this

XmlDocument doc = new XmlDocument();
doc.Load(path);

if (ListView1.SelectedIndex < doc.DocumentElement.ChildNodes.Count)
{
doc.DocumentElement.RemoveChild(doc.DocumentElement.ChildNodes[ListView1.SelectedIndex]);
doc.Save(path);
}

Delete Element from XML file using python

Based on your most recent edit, I think you'll find the problem is that your for loop isn't matching any nodes. Your document doesn't contain any elements named component or structuredBody. The xmlns="urn:hl7-org:v3" declaration on the root element mean that all elements in the document exist by default in that particular namespace, so you need to use that namespace when matching elements:

from lxml import objectify, etree

cda_tree = etree.parse('data.xml')
cda_root = cda_tree.getroot()

ns = {
'hl7': 'urn:hl7-org:v3',
}

for node in cda_tree.xpath('//hl7:component/hl7:structuredBody', namespaces=ns):
node.getparent().remove(node)

cda_tree.write('newXML.xml')

With the above code, if the input looks like this:

<ClinicalDocument
xmlns="urn:hl7-org:v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<component>
<structuredBody>
...
...
</structuredBody>
</component>
</ClinicalDocument>

The output looks like:

<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<component>
</component>
</ClinicalDocument>

How do I remove XML elements without removing content from the elements' tail?

What unintentionally gets removed here is not attributes, but the contents of an element's tail.

The tail property is a peculiarity of the ElementTree API. It is text immediately following an element's end tag and before any other tag. When you remove an element (in this case col), you also remove its tail.

The clearest explanation that I have found is this: http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/etree-view.html.


To get the wanted output, you need to keep a reference to the tail of the removed col element and append it to the text of the parent element. A complete example:

from xml.etree import ElementTree as ET

XML = """
<root>
<p id="S6CV0001P0-00507"><member>The Minister for Overseas Development (Mr. Neil Marten)
</member><membercontribution>: a policy
<col>16</col>
foobar barforb </membercontribution></p>
</root>
"""

root = ET.fromstring(XML)

for pid in root.findall(".//p"):
for cont in pid.findall('membercontribution'):
for col in cont.findall('col'):
col_tail = col.tail.strip() # Get the tail of "col"
cont.remove(col) # Remove "col"
cont.text = cont.text.strip() + " " # Replace trailing whitespace with single space
cont.text = cont.text + col_tail # Add the tail to "membercontribution"

print ET.tostring(root)

Output:

<root>
<p id="S6CV0001P0-00507"><member>The Minister for Overseas Development (Mr. Neil Marten)
</member><membercontribution>: a policy foobar barforb</membercontribution></p>
</root>

PHP - Delete XML Element

You can use the DOM classes in PHP. ( http://us3.php.net/manual/en/intro.dom.php ).

You will need to read the XML document into memory, use the DOM classes to do manipulation, and then you can save out the XML as needed (to http or to file).

DOMNode is an object in there that has remove features (to address your question).

It's a little more complicated than SimpleXML but once you get used to it, it's much more powerful

(semi-taken from a code example at php.net)

<?php

$doc = new DOMDocument;
$doc->load('theFile.xml');

$thedocument = $doc->documentElement;

//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('message');

//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
$attrValue = $domElement->getAttribute('time');
if ($attrValue == 'VALUEYOUCAREABOUT') {
$nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
}
}

//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

echo $doc->saveXML();
?>

This should give you a little bit of an idea on how to remove the element. It will print out the XML without that node. If you wanted to send it to file, just write the string to file.

How to remove elements from a xml file based on the value of a sub element using ansible?

Q: "Remove an item who's label's value (i.e. SOME_TEXT) matches a string label_of_item_to_remove"

Yes. It is possible. For example, given the file

shell> cat /tmp/test.xml 
<?xml version='1.0' encoding='UTF-8'?>
<doc>
<item>
<label>SOME_TEXT</label>
<other_elements/>
</item>
<item>
<label>OTHER_TEXT</label>
<other_elements/>
</item>
</doc>

the task below will remove the first item

    - community.general.xml:
path: /tmp/test.xml
xpath: "/doc/item[label='{{ label_of_item_to_remove }}']"
state: absent
vars:
label_of_item_to_remove: SOME_TEXT

Running the play with options --check --diff shows

TASK [community.general.xml] *********************************************
--- before
+++ after
@@ -1,9 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<doc>
-<item>
- <label>SOME_TEXT</label>
- <other_elements/>
-</item>
<item>
<label>OTHER_TEXT</label>
<other_elements/>

Remove node from XML node based an attribute value in Python

See below

from xml.etree import ElementTree as ET

xml = """<groups>
<group>
<group_components>
<id item="1">14742</id>
<id item="1">121727</id>
<id item="0">541971</id>
</group_components>
</group>
<group>
<group_components>
<id item="1">10186</id>
<id item="1">10553</id>
<id item="1">10644</id>
<id item="0">434639</id>
</group_components>
</group>
</groups>
"""
root = ET.fromstring(xml)
for grp_comp in root.findall('.//group_components'):
for _id in list(grp_comp):
if _id.attrib['item'] == "1":
grp_comp.remove(_id)
ET.dump(root)

output

<groups>
<group>
<group_components>
<id item="0">541971</id>
</group_components>
</group>
<group>
<group_components>
<id item="0">434639</id>
</group_components>
</group>
</groups>

Remove element in a XML file with Python

I took your code for a spin but at first Python couldn't agree with the way you composed your XML, wanting the / in the closing tag to be at the beginning (like </...>) instead of at the end (<.../>).

That aside, the reason your code isn't working is because the xpath expression is looking for the attribute openingHour while in reality you want to look for elements called openingHours. I got it to work by changing the expression to //openingHours. Making the entire code:

from lxml import etree

doc=etree.parse('stations.xml')
for elem in doc.xpath('//openingHours'):
parent = elem.getparent()
parent.remove(elem)
print(etree.tostring(doc))


Related Topics



Leave a reply



Submit