使用 minidom 解析 XML

发布于 2024-11-25 13:49:10 字数 833 浏览 0 评论 0原文

我正在与迷你统治作斗争。我需要在 dom 中找到一个条目,更新它所保存的文本,然后保存文件。到目前为止,我成功定位特定元素的唯一方法是通过非常明确、直接、硬编码的方法:

doc.childNodes[0].childNodes[3].childNodes[5].childNodes[11].childNodes[1].childNodes[3] 

我只想更新中的第一个

<typeBoxes>
    <typeBox type="counter">
        <text fontSize="140">123456</text>
        <text fontSize="26">Foobar</text>
        <incrementTextFieldNum>1</incrementTextFieldNum>
        <timing>1</timing>
        <increment>1</increment>
    </typeBox>
    <typeBox>
        <image>images/foo.png</image>
        <text fontSize="26">Foo</text>-->
    </typeBox>

    ...

建议?

I'm struggling with minidom. I need to locate an entry in the dom, update the text it holds, and then save the file. So far, the only way I've successfully been able to do locate the particular element is through a very explicit, direct, hard-coded method:

doc.childNodes[0].childNodes[3].childNodes[5].childNodes[11].childNodes[1].childNodes[3] 

I just want to update the first <text> in <typeBox type="counter">.

<typeBoxes>
    <typeBox type="counter">
        <text fontSize="140">123456</text>
        <text fontSize="26">Foobar</text>
        <incrementTextFieldNum>1</incrementTextFieldNum>
        <timing>1</timing>
        <increment>1</increment>
    </typeBox>
    <typeBox>
        <image>images/foo.png</image>
        <text fontSize="26">Foo</text>-->
    </typeBox>

    ...

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-12-02 13:49:10

minidom 不是很强大,现在大多数人都在使用 ElementTree 的一些变体。 Python2.5及更高版本已经内置了它。

>>> from xml.etree import ElementTree as etree
>>> corpus = """<typeBoxes>
...     <typeBox type="counter">
...         <text fontSize="140">123456</text>
...         <text fontSize="26">Foobar</text>
...         <incrementTextFieldNum>1</incrementTextFieldNum>
...         <timing>1</timing>
...         <increment>1</increment>
...     </typeBox>
...     <typeBox>
...         <image>images/foo.png</image>
...         <text fontSize="26">Foo</text>-->
...     </typeBox>
... </typeBoxes>"""
>>> 
>>> doc = etree.fromstring(corpus)
>>> 
>>> for typeBox in doc.findall('typeBox'):
...     if typeBox.attrib.get('type') == 'counter':
...         fieldnum = int(typeBox.find('incrementTextFieldNum').text)
...         incr = int(typeBox.find('increment').text)
...         text_field = typeBox.findall('text')[fieldnum-1]
...         text_field.text = str(int(text_field.text) + incr)
... 
>>> print etree.tostring(doc)
<typeBoxes>
    <typeBox type="counter">
        <text fontSize="140">123457</text>
        <text fontSize="26">Foobar</text>
        <incrementTextFieldNum>1</incrementTextFieldNum>
        <timing>1</timing>
        <increment>1</increment>
    </typeBox>
    <typeBox>
        <image>images/foo.png</image>
        <text fontSize="26">Foo</text>-->
    </typeBox>
</typeBoxes>
>>> 

minidom is not very powerful, most folks are using some variation of ElementTree these days. Python2.5 and later has it built in.

>>> from xml.etree import ElementTree as etree
>>> corpus = """<typeBoxes>
...     <typeBox type="counter">
...         <text fontSize="140">123456</text>
...         <text fontSize="26">Foobar</text>
...         <incrementTextFieldNum>1</incrementTextFieldNum>
...         <timing>1</timing>
...         <increment>1</increment>
...     </typeBox>
...     <typeBox>
...         <image>images/foo.png</image>
...         <text fontSize="26">Foo</text>-->
...     </typeBox>
... </typeBoxes>"""
>>> 
>>> doc = etree.fromstring(corpus)
>>> 
>>> for typeBox in doc.findall('typeBox'):
...     if typeBox.attrib.get('type') == 'counter':
...         fieldnum = int(typeBox.find('incrementTextFieldNum').text)
...         incr = int(typeBox.find('increment').text)
...         text_field = typeBox.findall('text')[fieldnum-1]
...         text_field.text = str(int(text_field.text) + incr)
... 
>>> print etree.tostring(doc)
<typeBoxes>
    <typeBox type="counter">
        <text fontSize="140">123457</text>
        <text fontSize="26">Foobar</text>
        <incrementTextFieldNum>1</incrementTextFieldNum>
        <timing>1</timing>
        <increment>1</increment>
    </typeBox>
    <typeBox>
        <image>images/foo.png</image>
        <text fontSize="26">Foo</text>-->
    </typeBox>
</typeBoxes>
>>> 
痴者 2024-12-02 13:49:10

如果您想查找 childNodes 列表中的第一项,请尝试使用:

typeBox_node = next((node for node in typeBoxs_node.childNodes \
                              if node.localName == 'typeBox'))

如果您想对某个元素中的每个元素执行此操作,请尝试:

for typeBox_node in doc.getElementsByTagName('typeBox'):
    text_node = next((node for node in typeBox_node.childNodes \
                              if node.localName == 'text'))

If you want to find the first item in a childNodes list, try using:

typeBox_node = next((node for node in typeBoxs_node.childNodes \
                              if node.localName == 'typeBox'))

If you want to do it for each one of some element, try:

for typeBox_node in doc.getElementsByTagName('typeBox'):
    text_node = next((node for node in typeBox_node.childNodes \
                              if node.localName == 'text'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文