Sponsored Content
Top Forums Shell Programming and Scripting Only print specific xml values that meet two criteria in python Post 302990125 by brianjb on Sunday 22nd of January 2017 01:09:32 PM
Old 01-22-2017
Only print specific xml values that meet two criteria in python

I have a large XML file that I want to parse, and only print one specific value if two values are met.

This is the code so far:

Code:
#!/usr/local/bin/python

   import xml.etree.ElementTree as ET
   tree = ET.parse('onedb-dhcp.xml')
   root = tree.getroot()

   # This successfully gets all items in the xml:

   print 'This successfully gets all items in the xml:\n'

   for p in root.iter('PROPERTY'):
   	print p.attrib
   print '\n----------------------------------------------------------'



This is the sample xml file:

Code:
<DATABASE NAME="test" VERSION="43-39" MD5="." SCHEMA-MD5="." INT-VERSION="43-39">
   <OBJECT><PROPERTY NAME="__type" VALUE="dhcp.lease"/><PROPERTY NAME="is_invalid_mac" VALUE="false"/><PROPERTY NAME="deferred_ttl" VALUE="300"/><PROPERTY NAME="ack_state" VALUE="renew"/><PROPERTY NAME="v6_prefix_bits" VALUE="0"/><PROPERTY NAME="is_ipv4" VALUE="true"/><PROPERTY NAME="vnode_id" VALUE="79"/><PROPERTY NAME="node_id" VALUE="79"/><PROPERTY NAME="ip_address" VALUE="10.10.1.6"/><PROPERTY NAME="dhcp_range" VALUE="10.10.1.5/10.10.1.254///0/"/><PROPERTY NAME="network_view" VALUE="0"/><PROPERTY NAME="starts" VALUE="2 2017/01/17 04:58:52"/><PROPERTY NAME="ends" VALUE="6 2017/01/21 04:58:52"/><PROPERTY NAME="tstp" VALUE="1 2017/01/23 04:58:52"/><PROPERTY NAME="tsfp" VALUE="1 2017/01/23 04:58:52"/><PROPERTY NAME="atsfp" VALUE="1 2017/01/23 04:58:52"/><PROPERTY NAME="cltt" VALUE="2 2017/01/17 04:58:52"/><PROPERTY NAME="hardware" VALUE="00:1a:4b:26:fd:85"/><PROPERTY NAME="client_hostname" VALUE="&quot;printer1&quot;"/><PROPERTY NAME="binding_state" VALUE="active"/><PROPERTY NAME="next_binding_state" VALUE="expired"/><PROPERTY NAME="variable" VALUE="vendor-class-identifier=&quot;Hewlett-Packard JetDirect&quot; ddns-fwd-name=&quot;printer1.testing.net&quot; ddns-rev-name=&quot;6.1.10.10.in-addr.arpa.&quot; ddns-txt=&quot;0015dce5883b53fa75c8d90d1312f0c054&quot; lt=&quot;04294967295&quot;"/><PROPERTY NAME="ms_server_id" VALUE="."/><PROPERTY NAME="fingerprint" VALUE="HP Printer"/><PROPERTY NAME="fingerprint_class" VALUE="Printers"/></OBJECT>
   <OBJECT><PROPERTY NAME="__type" VALUE="dhcp.lease"/><PROPERTY NAME="is_invalid_mac" VALUE="false"/><PROPERTY NAME="deferred_ttl" VALUE="300"/><PROPERTY NAME="ack_state" VALUE="from_peer"/><PROPERTY NAME="v6_prefix_bits" VALUE="0"/><PROPERTY NAME="is_ipv4" VALUE="true"/><PROPERTY NAME="vnode_id" VALUE="86"/><PROPERTY NAME="node_id" VALUE="86"/><PROPERTY NAME="ip_address" VALUE="10.10.1.44"/><PROPERTY NAME="dhcp_range" VALUE="10.10.1.5/101.10.1.254///0/"/><PROPERTY NAME="network_view" VALUE="0"/><PROPERTY NAME="starts" VALUE="2 2017/01/17 04:58:52"/><PROPERTY NAME="ends" VALUE="6 2017/01/21 04:58:52"/><PROPERTY NAME="tstp" VALUE="4 2016/06/23 19:17:54"/><PROPERTY NAME="tsfp" VALUE="1 2017/01/23 04:58:52"/><PROPERTY NAME="atsfp" VALUE="1 2017/01/23 04:58:52"/><PROPERTY NAME="cltt" VALUE="5 2016/06/17 19:17:54"/><PROPERTY NAME="hardware" VALUE="00:1a:4b:26:fd:85"/><PROPERTY NAME="client_hostname" VALUE="&quot;printer2&quot;"/><PROPERTY NAME="binding_state" VALUE="active"/><PROPERTY NAME="next_binding_state" VALUE="expired"/><PROPERTY NAME="variable" VALUE="lt=&quot;345600&quot; ddns-txt=&quot;0015dce5883b53fa75c8d90d1312f0c054&quot; ddns-rev-name=&quot;44.1.10.10.in-addr.arpa.&quot; ddns-fwd-name=&quot;printer2.testing.net&quot; vendor-class-identifier=&quot;Hewlett-Packard JetDirect&quot;"/><PROPERTY NAME="ms_server_id" VALUE="."/></OBJECT>
   </DATABASE>


When I run the above script, this is what I get printed to screen (just a sample):

Code:
{'NAME': '__type', 'VALUE': 'dhcp.lease'}
   {'NAME': 'is_invalid_mac', 'VALUE': 'false'}
   {'NAME': 'deferred_ttl', 'VALUE': '300'}
   {'NAME': 'ack_state', 'VALUE': 'renew'}
   {'NAME': 'v6_prefix_bits', 'VALUE': '0'}
   {'NAME': 'is_ipv4', 'VALUE': 'true'}
   {'NAME': 'vnode_id', 'VALUE': '79'}
   {'NAME': 'node_id', 'VALUE': '79'}
   {'NAME': 'ip_address', 'VALUE': '10.10.1.6'}


How can I set it up to only print the 'ip_address' value if '_type' = 'dhcp.lease'

I've tried this:

Code:
l = 'dhcp.lease'
   ip = 'ip_address'

   for s in root.iter('PROPERTY'):
           n = s.attrib['NAME']
           d = s.attrib['VALUE']
           if d == l:
                   print s.attrib['VALUE']

That prints out this:

Code:
Searching for specific things...

   dhcp.lease
   dhcp.lease



I think I'm close to the finish line, but need some help getting over it.

Thanks in advance!
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with egrep or grep command to meet multiple criteria

Hello, I"m a newbie :). I hope I can learn from the scripting expert. I'm trying to use egrep and grep commands to get the total count by meeting both criteria. So far, I haven't been able to do it. if robot = TLD and barcode = AA, then final count should be 2 if robot = TLD and... (9 Replies)
Discussion started by: MinBee
9 Replies

2. Shell Programming and Scripting

Need help to change values in XML using Python? noob help pls!

Hello I am a noob in XML and Python. I am trying to do this for my MSc project about a network simulation and need some help.... I want to change the values shown below: <num_crash_failures>1</num_crash_failures> −<crash_failure_entry> <freeze_at_slot>0</freeze_at_slot>... (0 Replies)
Discussion started by: erhanasd
0 Replies

3. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

4. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

5. UNIX for Dummies Questions & Answers

Print lines meet requirement

Dear Masters, I have 2 files input below file1 8269229289|CROATIA|LUX 8269229412|ASIA|LUX 8269229371|EUROPE|LUX 8269229355|LANE|LUX 8269229469|SWISS|LUX 8269229477|HAMBURG|LUX 8269229484|EGYPT|LUX 8269229485|GERMANY|LUX 8269229498|CROATIA|LUX File2 8269229289|1100100020... (6 Replies)
Discussion started by: radius
6 Replies

6. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

7. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

8. Shell Programming and Scripting

awk to print specific line in file based on criteria

In the file below I am trying to extract a specific instance of path, if the adjacent plugin": "/rundb/api/v1/plugin/49/. Thank you :). file "path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "plugin": "/rundb/api/v1/plugin/49/",... (8 Replies)
Discussion started by: cmccabe
8 Replies

9. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies
All times are GMT -4. The time now is 04:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy