Get XML children name,其子对象使用LXML只返回Non

2024-04-24 00:47:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用LXML格式解析一些XML

<?xml version="1.0" encoding="utf-8"?>
<ttFont sfntVersion="OTTO" ttLibVersion="2.5">

  <cmap>
    <tableVersion version="0"/>
    <cmap_format_4 platformID="0" platEncID="3" language="0">
      <map code="0x0" name=".null"/><!-- ???? -->
      <map code="0xd" name="CR"/><!-- ???? -->

基于教程here,但是由于某些原因

xmlFileName = "xml/myfile.ttx"
f = open(xmlFileName, "r")
s = f.read()

doc = etree.XML(s.strip())
map = doc.findtext('map')
print map

仅返回None。如何获取cmap的所有子节点(例如,cmap_format_4)和所有映射子节点的节点名?你知道吗


Tags: nameformatmapdoc节点version格式code
1条回答
网友
1楼 · 发布于 2024-04-24 00:47:14

使用findall()获取cmap_format_4节点的所有map子节点。示例:

for cmap_format in doc.findall('.//cmap_format_4'):
    for m in cmap_format.findall('map'):
        print m.attrib['code']

印刷品:

0x0
0xd

相关问题 更多 >