Re: XML file remove <car> sibling using elementTree and jython

Randall Wood <[email protected]> Sun, 1 Nov 2020 08:05:09 -0500
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
--===============7934945423494683520==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_E6DA33B4-668B-472B-81E4-AD2A4883E957"


--Apple-Mail=_E6DA33B4-668B-472B-81E4-AD2A4883E957
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Ask on the JMRI user=E2=80=99s group (https://groups.io/g/jmriusers =
<https://groups.io/g/jmriusers>); you probably want to use the JMRI APIs =
to find and delete the car instead of directly manipulating the XML =
content.

> On 01-Nov-2020, at 07:44, James Kelly <[email protected]> wrote:
>=20
> Win-10, Java 1.8, current JMRI version, NetBeans IDE, Jython, =
elementTree
> I'm pulling elements from OperationsCarRoster.xml and want to delete =
some siblings. Since all siblings have the same name <car> how can I =
select and delete a specific car using attributes?
> for example how do I use elementTree Remove(subelement) to delete a =
car with id=3D"MILW1001"=20
>=20
> i'm using Jython (Python) on Win-10 and elementTree
> here's all the code as I have it right now.
>=20
> import xml.etree.ElementTree as ET
> def __init__(self):
>             #def open_car_xml_file():
>             global trainTree
>             global trainRoot
>             trainTree =3D ET.parse('train.xml')
>             trainRoot =3D trainTree.getroot()
>             #self.add_train_rfid_tags()
>             print trainRoot
>=20
> def drop_car(self, car, engine, location):
>         for elem in trainRoot:
>             for subelem in elem.findall('car'):
>                 indentifier =3D str(subelem.get('engine', =
default=3DNone))
>                 if indentifier =3D=3D engine:
>                     indentifier2 =3D str(subelem.get('id', =
default=3DNone))
>                     if indentifier2 =3D=3D car:
>                         getDroppedCarData =3D [0,1,2,3,4,5,6,7]
>                         getDroppedCarData[0] =3D subelem.get('id', =
default=3DNone)
>                         getDroppedCarData[1] =3D =
subelem.get('roadName', default=3DNone)
>                         getDroppedCarData[2] =3D =
subelem.get('roadNumber', default=3DNone)
>                         getDroppedCarData[3] =3D subelem.get('type', =
default=3DNone)
>                         getDroppedCarData[4] =3D subelem.get('color', =
default=3DNone)
>                         getDroppedCarData[5] =3D subelem.get('rfid', =
default=3DNone)
>                         getDroppedCarData[6] =3D =
subelem.get('locationId', default=3DNone)
>                         getDroppedCarData[7] =3D subelem.get('owner', =
default=3DNone)
>                         (how do I using remove() to delete this =
record?
> =20
>     return getDroppedCarData=20
> _______________________________________________
> Jython-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jython-users


--Apple-Mail=_E6DA33B4-668B-472B-81E4-AD2A4883E957
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D"">Ask =
on the JMRI user=E2=80=99s group (<a =
href=3D"https://groups.io/g/jmriusers" =
class=3D"">https://groups.io/g/jmriusers</a>); you probably want to use =
the JMRI APIs to find and delete the car instead of directly =
manipulating the XML content.<br class=3D""><div><br =
class=3D""><blockquote type=3D"cite" class=3D""><div class=3D"">On =
01-Nov-2020, at 07:44, James Kelly &lt;<a =
href=3D"mailto:[email protected]" class=3D"">[email protected]</a>&gt;=
 wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><div =
dir=3D"ltr" class=3D""><div class=3D"gmail_default" =
style=3D"font-size:large"><span =
style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">Win-10, =
Java 1.8, current JMRI version, NetBeans IDE, Jython, =
elementTree</span><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D""><span style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">I'm =
pulling elements from OperationsCarRoster.xml and want to delete some =
siblings. Since all siblings have the same name &lt;car&gt; how can I =
select and delete a specific car using attributes?</span><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D""><span style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">for =
example how do I use elementTree Remove(subelement) to delete a car =
with&nbsp;</span><span =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">id=3D"MILW1001"</span>&nbsp;</div><div class=3D"gmail_default" =
style=3D"font-size:large"><br class=3D""></div><span =
style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">i'm =
using Jython (Python) on Win-10 and elementTree</span><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D""><span style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">here's =
all the code as I have it right now.</span><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D""><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D""><span style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">import =
xml.etree.ElementTree as ET</span><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D""><div=
 =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">def =
__init__(self):</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #def =
open_car_xml_file():</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global =
trainTree</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global =
trainRoot</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trainTree =3D =
ET.parse('train.xml')</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trainRoot =3D =
trainTree.getroot()</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
#self.add_train_rfid_tags()</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print =
trainRoot</div><br =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D""><div=
 =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">def =
drop_car(self, car, engine, location):</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; for elem in trainRoot:</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for subelem in =
elem.findall('car'):</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
indentifier =3D str(subelem.get('engine', default=3DNone))</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if =
indentifier =3D=3D engine:</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; indentifier2 =3D str(subelem.get('id', =
default=3DNone))</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; if indentifier2 =3D=3D car:</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData =3D =
[0,1,2,3,4,5,6,7]</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[0] =3D subelem.get('id', =
default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[1] =3D =
subelem.get('roadName', default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[2] =3D =
subelem.get('roadNumber', default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[3] =3D subelem.get('type', =
default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[4] =3D =
subelem.get('color', default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[5] =3D subelem.get('rfid', =
default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[6] =3D =
subelem.get('locationId', default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; getDroppedCarData[7] =3D =
subelem.get('owner', default=3DNone)</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; (how do I using remove() to delete this =
record?</div><div =
style=3D"box-sizing:border-box;color:rgb(51,51,51);font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" =
class=3D"">&nbsp;</div><div class=3D"gmail_default" =
style=3D"font-size:large"><span =
style=3D"color:rgb(51,51,51);font-family:&quot;Helvetica =
Neue&quot;,Helvetica,Arial,sans-serif;font-size:14px" class=3D"">&nbsp; =
&nbsp; return getDroppedCarData</span>&nbsp;<br class=3D""></div></div>
_______________________________________________<br class=3D"">Jython-users=
 mailing list<br class=3D""><a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a><br =
class=3D"">https://lists.sourceforge.net/lists/listinfo/jython-users<br =
class=3D""></div></blockquote></div><br class=3D""></body></html>=

--Apple-Mail=_E6DA33B4-668B-472B-81E4-AD2A4883E957--


--===============7934945423494683520==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============7934945423494683520==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users

--===============7934945423494683520==--