我有以下XML:
<s:dict xmlns:s="http://dev.splunk.com/ns/rest" xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<s:key name="_rcvbuf">1572864</s:key>
<s:key name="connection_host">ip</s:key>
<s:key name="disabled">0</s:key>
<s:key name="eai:acl">
<s:dict>
<s:key name="app">search</s:key>
<s:key name="can_list">1</s:key>
<s:key name="can_write">1</s:key>
<s:key name="modifiable">0</s:key>
<s:key name="owner">nobody</s:key>
<s:key name="perms">
<s:dict>
<s:key name="read">
<s:list>
<s:item>admin</s:item>
<s:item>power</s:item>
<s:item>splunk-system-role</s:item>
<s:item>user</s:item>
</s:list>
</s:key>
<s:key name="write">
<s:list>
<s:item>admin</s:item>
<s:item>splunk-system-role</s:item>
</s:list>
</s:key>
</s:dict>
</s:key>
<s:key name="removable">1</s:key>
<s:key name="sharing">app</s:key>
</s:dict>
</s:key>
<s:key name="evt_dc_name"/>
<s:key name="evt_dns_name"/>
<s:key name="evt_resolve_ad_obj">0</s:key>
<s:key name="group">listenerports</s:key>
<s:key name="host">Tyler-PC</s:key>
<s:key name="index">default</s:key>
<s:key name="showZeroValue">1</s:key>
</s:dict>
LXML似乎在解析这个时遇到了问题,因为孩子们已经“破碎”了:
In [137]: d.getchildren() #this is a loaded version of the XML above
Out[137]:
[1572864,
'ip',
0,
<Element {http://dev.splunk.com/ns/rest}key at 0x3f3ad78>,
'',
'',
0,
'listenerports',
'Tyler-PC',
'default',
1]
但是,LXML确实知道结构,因为我调用了descendantpaths,我得到了一个准确的结构。
In [138]: d.descendantpaths()
Out[138]:
['{http://dev.splunk.com/ns/rest}dict',
'{http://dev.splunk.com/ns/rest}dict.key',
'{http://dev.splunk.com/ns/rest}dict.key[1]',
'{http://dev.splunk.com/ns/rest}dict.key[2]',
'{http://dev.splunk.com/ns/rest}dict.key[3]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[1]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[2]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[3]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[4]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key.list',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key.list.item',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key.list.item[1]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key.list.item[2]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key.list.item[3]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key[1]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key[1].list',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key[1].list.item',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[5].dict.key[1].list.item[1]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[6]',
'{http://dev.splunk.com/ns/rest}dict.key[3].dict.key[7]',
'{http://dev.splunk.com/ns/rest}dict.key[4]',
'{http://dev.splunk.com/ns/rest}dict.key[5]',
'{http://dev.splunk.com/ns/rest}dict.key[6]',
'{http://dev.splunk.com/ns/rest}dict.key[7]',
'{http://dev.splunk.com/ns/rest}dict.key[8]',
'{http://dev.splunk.com/ns/rest}dict.key[9]',
'{http://dev.splunk.com/ns/rest}dict.key[10]']