Python lxml&从xpath开始?

时间:2016-10-24 21:07:42

标签: python xml xpath lxml

Python的新手和XML的新手,更不用说尝试解析它了。我需要从一系列weblogic XML配置文件中识别集群节点。我已经想出如何获得75%的人;现在,我正在追查边缘情况,我不确定如何继续。

Weblogic XML配置文件以命名空间定义开头,然后是一些子元素,其中一些子元素具有自己的子元素。

我感兴趣的元素是<server>,它通常会有一个名为<listen-address>的子元素,其中包含我正在寻找的主机名。

遵循“我们喜欢标准,我们有很多标准”的范例,这种模式无处不在。如果它不起作用,我需要查找名为<server>的{​​{1}}子元素。该元素包含一个别名,该别名在不同的根子项中展开,与<machine>处于同一级别。

所以,图片价值1000字:

注意:试图避免发送大量的xml;但是,显然,我对带宽节省的尝试使问题更难回答。道歉。我确实减少了不需要或与安全相关的子元素。

<server>

所以,在'普通'配置上运行它,我得到:

<?xml version='1.0' encoding='UTF-8'?>
<domain xmlns="http://xmlns.oracle.com/weblogic/domain" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/security/wls http://xmlns.oracle.com/weblogic/security/wls/1.0/wls.xsd http://xmlns.oracle.com/weblogic/domain http://xmlns.oracle.com/weblogic/1.0/domain.xsd http://xmlns.oracle.com/weblogic/security/xacml http://xmlns.oracle.com/weblogic/security/xacml/1.0/xacml.xsd http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator/1.0/passwordvalidator.xsd http://xmlns.oracle.com/weblogic/security http://xmlns.oracle.com/weblogic/1.0/security.xsd">
  <name>EDIServices_Domain</name>
  <domain-version>12.2.1.0.0</domain-version>
  <security-configuration>
    <name>EDIServices_Domain</name>
    <default-realm>myrealm</default-realm>
    <node-manager-username>snipped</node-manager-username>
    <node-manager-password-encrypted>not_really_my_pwd</node-manager-password-encrypted>
  </security-configuration>
  <server>
    <name>EDIServices_AS</name>
    <listen-port>60010</listen-port>
    <web-server>
      <name>EDIServices_AS</name>
      <web-server-log>
        <file-name>/opt/app/oracle/user_projects/logs/EDIServices_Domain/access.log</file-name>
        <file-count>24</file-count>
        <file-min-size>10000</file-min-size>
        <rotate-log-on-startup>true</rotate-log-on-startup>
        <elf-fields>c-ip cs-uri date time cs-method cs-uri sc-status</elf-fields>
        <log-file-format>common</log-file-format>
      </web-server-log>
    </web-server>
    <listen-address></listen-address>
  </server>
  <server>
    <name>EDIServices_MS1</name>
    <machine>EDIServices_MC1</machine>
    <listen-port>60014</listen-port>
    <cluster>EDIServices_CS</cluster>
    <listen-address></listen-address>
  </server>
  <server>
    <name>EDIServices_MS2</name>
    <machine>EDIServices_MC2</machine>
    <listen-port>60014</listen-port>
    <cluster>EDIServices_CS</cluster>
    <listen-address></listen-address>
  </server>
  <cluster>
    <name>EDIServices_CS</name>
    <cluster-messaging-mode>unicast</cluster-messaging-mode>
    <dynamic-servers>
      <maximum-dynamic-server-count>0</maximum-dynamic-server-count>
    </dynamic-servers>
  </cluster>
  <production-mode-enabled>true</production-mode-enabled>
  <configuration-version>12.2.1.0.0</configuration-version>
<machine xsi:type="unix-machineType">
    <name>EDIServices_MC1</name>
    <node-manager>
      <name>EDIServices_MC1</name>
      <nm-type>SSL</nm-type>
      <listen-address>host001</listen-address>
      <listen-port>7001</listen-port>
    </node-manager>
  </machine>
  <machine xsi:type="unix-machineType">
    <name>EDIServices_MC2</name>
    <node-manager>
      <name>EDIServices_MC2</name>
      <listen-address>host002</listen-address>
      <listen-port>7001</listen-port>
    </node-manager>
  </machine>
</domain>

针对abi-normal配置运行它,我现在正在:

$ ./lxml configs/EntsvcSoa_Domain_config.xml  
EntsvcSoa_AS    => host003.myco.com
EntsvcSoa_MS1   => host004.myco.com
EntsvcSoa_MS2   => host005.myco.com

使用上面的例子,我想分别将EDIServices_MC1和EDIServices_MC2翻译成host001和host002。

主循环是:

注意:为了完成,这是整个脚本:

$ ./lxml configs/EDIServices_Domain_config.xml
EDIServices_MS1 => EDIServices_MC1
EDIServices_MS2 => EDIServices_MC2

(我花了写了12行代码......我不这样做是件好事:))

改写,我需要找到名字与相应#!/usr/bin/env python3 from lxml import etree import re import sys import os import pprint if len(sys.argv) != 2: print('Format: ./wl_clusters ${weblogic_config_file}') sys.exit(1) if not os.path.isfile(sys.argv[1]): print('Format: ./wl_clusters ${weblogic_config_file}') sys.exit(2) config = sys.argv[1] # set up lxml structures tree = etree.parse(config) root = tree.getroot() # set up xml namespace govno ns = root.nsmap[None] namespaces = { 'ns': ns } for server in root.findall('ns:server', namespaces): cs = server.find('ns:cluster', namespaces) if cs is None: continue # cluster_name = server.find('ns:cluster', namespaces).text cluster_name = cs.text listen_address = server.find('ns:listen-address', namespaces) server_name = listen_address.text if server_name is None: machine = server.find('ns:machine', namespaces) if machine is None: continue else: server_name = machine.text print("%-15s => %s" % (cluster_name, server_name)) 孩子下的名字相匹配的<machine>孩子。从网上的一些例子来看,我相信xpath可能有所帮助,但我甚至无法让简单的例子工作。

1 个答案:

答案 0 :(得分:-1)

以下是将xpath与您的文件一起使用的示例。我处理每个元素&#34;域&#34;并显示&#34; *和#34;的名称,因为它们在同一级别内有效。

from lxml import etree
xp = etree.XPath('./*/name')
for event, elem in etree.iterparse("delme.xml", events=('end',), tag="domain"):
    for name in xp(elem):
        print(name.text)

输出:

EDIServices_MS1
EDIServices_MS2
EDIServices_MC1
EDIServices_MC2

编辑:创建粗略的xpath以匹配两种模式。