Python 使用lxml解析xml文件

Python 使用lxml解析xml文件,python,xml,lxml,Python,Xml,Lxml,我试图通过查找每个Watts标记并更改其中的文本来编辑xml文件。到目前为止,我已经成功地更改了所有标签,但没有具体更改Watts标签 我的解析器是: from lxml import etree tree = etree.parse("cycling.xml") root = tree.getroot() for watt in root.iter(): if watt.tag == "Watts": watt.text = "strong" tree.write(

我试图通过查找每个Watts标记并更改其中的文本来编辑xml文件。到目前为止,我已经成功地更改了所有标签,但没有具体更改Watts标签

我的解析器是:

from lxml import etree
tree = etree.parse("cycling.xml")
root = tree.getroot()

for watt in root.iter():
    if watt.tag == "Watts":
        watt.text = "strong"

tree.write("output.xml")
这使我的cycling.xml文件保持不变。output.xml中的代码段(由于未更改,因此也是cycling.xml文件)为:

然后,我的output.xml文件变成:

<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">strong<Activities>strong<Activity Sport="Biking">strong<Id>strong</Id>
      <Lap StartTime="2018-05-06T20:49:56Z">strong<TotalTimeSeconds>strong</TotalTimeSeconds>
        <DistanceMeters>strong</DistanceMeters>
        <MaximumSpeed>strong</MaximumSpeed>
        <Calories>strong</Calories>
        <Intensity>strong</Intensity>
        <TriggerMethod>strong</TriggerMethod>
        <Track>strong<Trackpoint>strong<Time>strong</Time>
            <Position>strong<LatitudeDegrees>strong</LatitudeDegrees>
              <LongitudeDegrees>strong</LongitudeDegrees>
            </Position>
            <HeartRateBpm>strong<Value>strong</Value>
            </HeartRateBpm>
            <Extensions>strong<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2">strong<Watts>strong</Watts>
                <Speed>strong</Speed>
              </TPX>
            </Extensions>
          </Trackpoint>
          <Trackpoint>strong<Time>strong</Time>
            <Position>strong<LatitudeDegrees>strong</LatitudeDegrees>
              <LongitudeDegrees>strong</LongitudeDegrees>
            </Position>
            <AltitudeMeters>strong</AltitudeMeters>
            <HeartRateBpm>strong<Value>strong</Value>
            </HeartRateBpm>
            <Extensions>strong<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2">strong<Watts>strong</Watts>
                <Speed>strong</Speed>
              </TPX>
            </Extensions>
          </Trackpoint>
strongstrong
坚强的
坚强的
坚强的
坚强的
坚强的
坚强的
strongstrongstrong
坚强的
坚强的
坚强的
strongstrongstrong
坚强的
坚强的
坚强的
坚强的
坚强的
坚强的
strongstrongstrong
坚强的
  • 我怎样才能只更改瓦茨标签
  • 我不明白
    root=tree.getroot()的作用。我只是想同时问这个问题,虽然我不确定这对我的问题是否重要

  • 您的文档定义了默认的XML命名空间。查看开始标记末尾的
    xmlns=
    属性:

    <TrainingCenterDatabase
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
    
    考虑到这一点,您可以修改过滤器,使其看起来像 这:


    您可以在.

    中阅读更多关于命名空间处理的替代资料,因为您使用两个重要的词——编辑XML和使用<代码> LXML >,考虑(XML转换语言),您可以在其中定义命名空间前缀,并在文档中更改WATS中的任何位置而不进行循环。另外,您还可以将值从Python传递到XSLT

    XSLT(另存为.xsl文件)

    
    
    Python

    from lxml import etree
    
    # LOAD XML AND XSL
    doc = etree.parse("cycling.xml")
    xsl = etree.parse('XSLT_Script.xsl')
    
    # CONFIGURE TRANSFORMER
    transform = etree.XSLT(xsl)    
    
    # RUN TRANSFORMATION WITH PARAM
    n = etree.XSLT.strparam('Strong')
    result = transform(doc, python_value=n)
    
    # PRINT TO CONSOLE
    print(result) 
    
    # SAVE TO FILE
    with open('Output.xml', 'wb') as f:
        f.write(result)
    

    非常感谢。我已经打印了watt.tag,但我不确定它告诉了我什么,并且我很难理解名称空间的某些部分。
    <TrainingCenterDatabase
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
    
    $ python filter.py 
    {http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}TrainingCenterDatabase
    [...]
    {http://www.garmin.com/xmlschemas/ActivityExtension/v2}Watts
    {http://www.garmin.com/xmlschemas/ActivityExtension/v2}Speed
    
    from lxml import etree
    tree = etree.parse("cycling.xml")
    root = tree.getroot()
    
    for watt in root.iter():
        if watt.tag == "{http://www.garmin.com/xmlschemas/ActivityExtension/v2}Watts":
            watt.text = "strong"
    
    tree.write("output.xml")
    
    from lxml import etree
    
    # LOAD XML AND XSL
    doc = etree.parse("cycling.xml")
    xsl = etree.parse('XSLT_Script.xsl')
    
    # CONFIGURE TRANSFORMER
    transform = etree.XSLT(xsl)    
    
    # RUN TRANSFORMATION WITH PARAM
    n = etree.XSLT.strparam('Strong')
    result = transform(doc, python_value=n)
    
    # PRINT TO CONSOLE
    print(result) 
    
    # SAVE TO FILE
    with open('Output.xml', 'wb') as f:
        f.write(result)