使用lxml

时间:2017-08-03 12:56:57

标签: xml python-3.x lxml

我正在尝试迭代xml以查找旁边的文本: XML文件:

<?xml version="1.0"?>

-<rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1F5/junos">


-<route-information xmlns="http://xml.juniper.net/junos/15.1F5/junos-routing">


-<route-table>

<table-name>ACTIVEFIN-csp.inet.0</table-name>

<destination-count>33</destination-count>

<total-route-count>33</total-route-count>

<active-route-count>33</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>0</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>20</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>ADP-csp.inet.0</table-name>

<destination-count>76</destination-count>

<total-route-count>78</total-route-count>

<active-route-count>76</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>0</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>250</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>ATG-csp.inet.0</table-name>

<destination-count>77</destination-count>

<total-route-count>78</total-route-count>

<active-route-count>39</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>38</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>20</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>BARCLAYS-csp.inet.0</table-name>

<destination-count>8</destination-count>

<total-route-count>8</total-route-count>

<active-route-count>8</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>0</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>20</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>BATS-csp.inet.0</table-name>

<destination-count>376</destination-count>

<total-route-count>408</total-route-count>

<active-route-count>335</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>41</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>38.98.183.192/28</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>BGP</protocol-name>

<preference>170</preference>

<age junos:seconds="746468">1w1d 15:21:08</age>

<local-preference>1000</local-preference>

<learned-from>198.140.33.62</learned-from>

<as-path>65092 ? </as-path>

<validation-state>unverified</validation-state>


-<nh>

<selected-next-hop/>

<to>10.61.1.194</to>

<via>ae1.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>


-<nh>

<to>10.61.1.198</to>

<via>ae2.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>BOA-csp.inet.0</table-name>

<destination-count>3</destination-count>

<total-route-count>3</total-route-count>

<active-route-count>2</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>1</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>20</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>BOX-csp.inet.0</table-name>

<destination-count>111</destination-count>

<total-route-count>115</total-route-count>

<active-route-count>111</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>0</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>0.0.0.0/0</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>Static</protocol-name>

<preference>250</preference>

<age junos:seconds="25799480">42w4d 14:31:20</age>

<nh-type>Discard</nh-type>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>CAP.inet.0</table-name>

<destination-count>938</destination-count>

<total-route-count>1028</total-route-count>

<active-route-count>902</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>36</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>38.98.183.192/28</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>BGP</protocol-name>

<preference>170</preference>

<age junos:seconds="746468">1w1d 15:21:08</age>

<local-preference>1000</local-preference>

<learned-from>198.140.33.62</learned-from>

<as-path>65092 ? </as-path>

<validation-state>unverified</validation-state>


-<nh>

<selected-next-hop/>

<to>10.61.1.194</to>

<via>ae1.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>


-<nh>

<to>10.61.1.198</to>

<via>ae2.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>CAPTEST.inet.0</table-name>

<destination-count>753</destination-count>

<total-route-count>823</total-route-count>

<active-route-count>717</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>36</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>38.98.183.192/28</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>BGP</protocol-name>

<preference>170</preference>

<age junos:seconds="746468">1w1d 15:21:08</age>

<local-preference>1000</local-preference>

<learned-from>198.140.33.62</learned-from>

<as-path>65092 ? </as-path>

<validation-state>unverified</validation-state>


-<nh>

<selected-next-hop/>

<to>10.61.1.194</to>

<via>ae1.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>


-<nh>

<to>10.61.1.198</to>

<via>ae2.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>

</rt-entry>

</rt>

</route-table>


-<route-table>

<table-name>CBOE-csp.inet.0</table-name>

<destination-count>228</destination-count>

<total-route-count>240</total-route-count>

<active-route-count>186</active-route-count>

<holddown-route-count>0</holddown-route-count>

<hidden-route-count>42</hidden-route-count>


-<rt junos:style="brief">

<rt-destination>38.98.183.192/28</rt-destination>


-<rt-entry>

<active-tag>*</active-tag>

<current-active/>

<last-active/>

<protocol-name>BGP</protocol-name>

<preference>170</preference>

<age junos:seconds="746468">1w1d 15:21:08</age>

<local-preference>1000</local-preference>

<learned-from>198.140.33.62</learned-from>

<as-path>65092 ? </as-path>

<validation-state>unverified</validation-state>


-<nh>

<selected-next-hop/>

<to>10.61.1.194</to>

<via>ae1.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>


-<nh>

<to>10.61.1.198</to>

<via>ae2.0</via>

<lsp-name>MAH-H1-CNR01-to-350C-P1-CNR01-DN</lsp-name>

</nh>

</rt-entry>

</rt>

</route-table>

-

{主}

使用下面的代码,我可以单值。但我想迭代xml文件,满足不同的条件:

 XML_NAMESPACE = '{http://xml.juniper.net/junos/15.1F5/junos-routing}'
        with open('version1.xml') as fd:
            mydict = fd.read()
        tree = etree.parse('version1.xml')
    node = tree.iter(tag='{}preference'.format(XML_NAMESPACE))
            print (next(node).text)

条件: 使用特定文本(例如BARCLAYS-csp.inet.0)迭代表,并仅在*

时查找值

尝试findall,iter()。无法迭代所有表名值。

0 个答案:

没有答案