Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将xpath元素转换为XML?_Python_Xpath - Fatal编程技术网

Python 如何将xpath元素转换为XML?

Python 如何将xpath元素转换为XML?,python,xpath,Python,Xpath,我正在使用python xpath,需要将元素转换为XML 我发现了dir的方法,但没有找到任何合适的方法 ['__bool__', '__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt

我正在使用python xpath,需要将元素转换为XML

我发现了dir的方法,但没有找到任何合适的方法

['__bool__', '__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '_init', 'addnext', 'addprevious', 'append', 'attrib', 'base', 'clear', 'cssselect', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'getnext', 'getparent', 'getprevious', 'getroottree', 'index', 'insert', 'items', 'iter', 'iterancestors', 'iterchildren', 'iterdescendants', 'iterfind', 'itersiblings', 'itertext', 'keys', 'makeelement', 'nsmap', 'prefix', 'remove', 'replace', 'set', 'sourceline', 'tag', 'tail', 'text', 'values', 'xpath']
应用程序:

afile.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<applications >
  <application >
      <journalNumber > 1 < /journalNumber >
   </application >
   <application >
      <journalNumber > 2</journalNumber>
   </application >
</applications >

请参阅:

共享关键的html/xml片段原始表示如果您不介意,请解释或让我参考一个链接,解释formatapplication方法如何在此处工作?如果我发现它令人困惑,我的搜索结果没有任何结果。不,我对它很熟悉,尽管我通常使用f字符串;我只是不理解使用xml标记名应用程序的机制,在本例中,它是所有本地名称的替换字符串,让您获得预期的输出。这是OP的原始代码行,我只引用了它,我没有质疑它,因为他如何从树中获取元素与所问的问题无关//*[local name='{0}'].formatapplication产生了/*[local name='application'],这是有效的XPath。这不一定是我要做的,这表明OP的XML文档包含名称空间,并且他不知道如何正确处理它们,但正如我所说的,这不是重点。
    etxml = etree.parse(afile)
    root = etxml.getroot()

    value = root.xpath("//*[local-name() = '{0}']".format("application"))
etxml = etree.parse(afile)
root = etxml.getroot()

value = root.xpath("//*[local-name() = '{0}']".format("application"))

etree.tostring(value)