简体   繁体   English

python中的XML命名空间

[英]XML Namespace in python

i need to get few value such as 我需要获得一些价值,例如

  1. w:val="FFFF00" 瓦特:VAL = “FFFF00”
  2. w:val="28" 瓦特:VAL = “28”
  3. Declaration word 声明词

from the xml file please help me with code 从xml文件中请帮助我的代码

I have Tried this code 我已经尝试过此代码

import xml.etree.ElementTree as ET
tree = ET.parse('document.xml')
# root = tree.getroot()
ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

for char in tree.findall('w:color',ns):
        print(' |-->', char.text)

please help me with the code how do i use xml namespaces in python to get value 请帮助我提供代码,我如何在python中使用xml名称空间来获取价值
this is XML Code:- 这是XML代码:

<w:tc>
    <w:tcPr>
        <w:tcW w:w="1502" w:type="dxa"/>
    </w:tcPr>
    <w:p w:rsidP="00065164" w:rsidRDefault="006F598E" w:rsidRPr="009D1FCD" w:rsidR="006F598E">
        <w:pPr>
            <w:autoSpaceDE w:val="0"/>
            <w:autoSpaceDN w:val="0"/>
            <w:adjustRightInd w:val="0"/>
            <w:jc w:val="center"/>
            <w:rPr>
                <w:rFonts w:cstheme="minorHAnsi"/>
                <w:b/>
                <w:color w:val="FFFF00"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="009D1FCD">
            <w:rPr>
                <w:rFonts w:cstheme="minorHAnsi"/>
                <w:b/>
                <w:color w:val="FFFF00"/>
            </w:rPr>
            <w:t>YEAR</w:t>
        </w:r>
    </w:p>
</w:tc>

<w:p w:rsidP="00443849" w:rsidRDefault="00104308" w:rsidRPr="00383A2A" w:rsidR="00104308">
    <w:pPr>
        <w:pStyle w:val="NoSpacing"/>
        <w:rPr>
            <w:rFonts w:cstheme="minorHAnsi"/>
            <w:b/>
            <w:sz w:val="28"/>
            <w:szCs w:val="28"/>
        </w:rPr>
    </w:pPr>
    <w:r w:rsidRPr="00383A2A">
        <w:rPr>
            <w:rFonts w:cstheme="minorHAnsi"/>
            <w:b/>
            <w:sz w:val="28"/>
            <w:szCs w:val="28"/>
        </w:rPr>
        <w:t>Declaration:</w:t>
    </w:r>
</w:p>

Data can also be extract using xml.dom module. 也可以使用xml.dom模块提取数据。 here is the code: 这是代码:

from xml.dom import expatbuilder
document = expatbuilder.parse("sample.xml", False)
node = document.getElementsByTagName('w:p')
for i in node:
    color_list =  i.getElementsByTagName('w:color')
    wvalue_data =  [d.getAttribute('w:val') for d in color_list ]
    if  wvalue_data:
        print "color w:val :" , wvalue_data
    desc_list = i.getElementsByTagName('w:t')
   print "desc   :" ,[node.firstChild.data for node in desc_list]
    wsz_data = i.getElementsByTagName('w:sz')
    value_data = [data.getAttribute('w:val') for data in wsz_data ]
    if value_data:
        print "w:sz_value :" , value_data

Output will be as follows: 输出如下:

color w:val : [u'FFFF00', u'FFFF00']
desc   : [u'YEAR']
desc   : [u'Declaration:']
w:sz_value : [u'28', u'28']

Hope it is useful. 希望它是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM