- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个非常大的 .xml 文件,我正在尝试创建一个新的 .xml 文件,它只包含这个较大文件内容的一小部分。我想指定一个属性(在我的例子中,一个 itemID)并给它一些特定的值,然后它会去除所有元素,除了那些具有这些 itemID 及其子元素的元素。
我的大型 .xml 文件看起来像这样:
<?xml version='1.0' encoding='UTF-8'?>
<api version="2">
<currentTime>2013-02-27 17:00:18</currentTime>
<result>
<rowset name="assets" key="itemID" columns="itemID,locationID,typeID,quantity,flag,singleton">
<row itemID="1008551770576" locationID="31000559" typeID="17187" quantity="1" flag="0" singleton="1" rawQuantity="-1" />
<row itemID="1008700753886" locationID="31000559" typeID="17187" quantity="1" flag="0" singleton="1" rawQuantity="-1" />
<row itemID="1008700756994" locationID="31000559" typeID="17184" quantity="1" flag="0" singleton="1" rawQuantity="-1" />
<row itemID="1008701224901" locationID="31000559" typeID="17186" quantity="1" flag="0" singleton="1" rawQuantity="-1" />
<row itemID="1004072840841" locationID="31002238" typeID="17621" quantity="1" flag="0" singleton="1" rawQuantity="-1">
<rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
<row itemID="150571923" typeID="25863" quantity="2" flag="119" singleton="0" />
<row itemID="188435728" typeID="3388" quantity="1" flag="119" singleton="0" />
<row itemID="210122947" typeID="3419" quantity="4" flag="119" singleton="0" />
</rowset>
</row>
<row itemID="1005279202146" locationID="31002238" typeID="17621" quantity="1" flag="0" singleton="1" rawQuantity="-1">
<rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
<row itemID="1004239962001" typeID="16275" quantity="49596" flag="4" singleton="0" />
<row itemID="1005364142068" typeID="4246" quantity="156929" flag="4" singleton="0" />
<row itemID="1005624252854" typeID="4247" quantity="93313" flag="4" singleton="0" />
</rowset>
</row>
<row itemID="1004388226389" typeID="648" quantity="1" flag="0" singleton="1" rawQuantity="-1">
<rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
<row itemID="1004388228218" typeID="31119" quantity="1" flag="92" singleton="1" rawQuantity="-1" />
<row itemID="1004388701243" typeID="31119" quantity="1" flag="94" singleton="1" rawQuantity="-1" />
<row itemID="1004388701485" typeID="31119" quantity="1" flag="93" singleton="1" rawQuantity="-1" />
<row itemID="1009147502645" typeID="51" quantity="1" flag="5" singleton="1" rawQuantity="-1" />
</rowset>
</row>
</rowset>
</result>
<cachedUntil>2013-02-27 23:00:18</cachedUntil>
</api>
这个文件大约有九万行,大约 9 兆字节。
请注意 itemID 是如何存在的,某些项目类型可以(但不总是)在其中包含更多项目,并且这些子项也有自己的 itemID。我正在尝试获取一些特定的 itemID 及其子项,而忽略所有其他项。
我使用了 this answer 中的代码这让我很接近。它是完美的,除了它遗漏了我使用的 itemID 的 child 。
我的代码是这样的:
import lxml.etree as le
##Take this big .xml file and pull out just the parts we want then write those to a new .xml file##
with open(filename,'r') as f:
doc=le.parse(f)
for elem in doc.xpath('//*[attribute::itemID]'):
if elem.attrib['itemID']=='1004072840841':
elem.attrib.pop('itemID')
else:
parent=elem.getparent()
parent.remove(elem)
print(le.tostring(doc))
打印结果如下:
<api version="2">
<currentTime>2013-03-01 21:46:52</currentTime>
<result>
<rowset name="assets" key="itemID" columns="itemID,locationID,typeID,quantity,flag,singleton">
<row locationID="31002238" typeID="17621" quantity="1" flag="0" singleton="1" rawQuantity="-1">
<rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
</rowset>
</row>
</rowset>
</result>
<cachedUntil>2013-03-02 03:46:53</cachedUntil>
</api>
我希望它看起来像这样:
<api version="2">
<currentTime>2013-03-01 21:46:52</currentTime>
<result>
<rowset name="assets" key="itemID" columns="itemID,locationID,typeID,quantity,flag,singleton">
<row locationID="31002238" typeID="17621" quantity="1" flag="0" singleton="1" rawQuantity="-1">
<rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
<row itemID="150571923" typeID="25863" quantity="2" flag="119" singleton="0" />
<row itemID="188435728" typeID="3388" quantity="1" flag="119" singleton="0" />
<row itemID="210122947" typeID="3419" quantity="4" flag="119" singleton="0" />
</rowset>
</row>
</rowset>
</result>
<cachedUntil>2013-03-02 03:46:53</cachedUntil>
</api>
我对代码的理解还不够深入,看不出我需要更改哪些内容才能同时包含我让它搜索的 itemID 的子项。此外,理想情况下,我能够放入多个 itemID,它会去除除那些 itemID 及其子项之外的所有项。这意味着它需要保留 itemID=[number]
行属性(以便我在使用此 xml 文件时可以使用 xPath 来引用特定的 itemID 及其子项。)
所以我的主要问题是关于如何在生成的 .xml 中包含我搜索的 itemID 的子项。我的第二个问题是关于如何同时对多个 itemID 执行此操作(这样生成的 .xml 文件将删除除那些 itemID 及其子项之外的所有内容。)
我发现 elem.attrib.pop('itemID')
部分是取出 itemID 的部分,因为我想要多个 itemIDs 并且他们的 child 仍然存在,所以我需要为了保留这个,所以我把那个部分去掉了。我试图找到一种方法来跳过带有我正在搜索的 itemID 的行的子项,而我想到的是用一个属性标记每个子项,然后我可以重新搜索并删除所有不存在的属性有那个属性。我不需要 flag 属性来完成我正在做的事情,所以我继续使用它来达到这个目的(因为当我试图迭代它们时,尝试引入一个新属性遇到了一个关键错误。)只是标记 children 还不够,我还得标记 children 的 child 。
这是我丑陋的解决方案:
with open(filename,'r') as f:
doc=le.parse(f)
for elem in doc.xpath('//*[attribute::itemID]'):
if elem.attrib['itemID']=='1004072840841' or elem.attrib['itemID']=='1005279202146': # this or statement lets me get a resulting .xml file that has two itemIDs and their children
elem.attrib['flag']='Keep'
for child in elem.iterchildren():
child.attrib['flag']='Keep'
for c in child.iterchildren():
c.attrib['flag']='Keep'
else:
pass
for e in doc.xpath('//*[attribute::flag]'):
if e.attrib['flag']!='Keep':
parent=e.getparent()
parent.remove(e)
else:
pass
print(le.tostring(doc))
##This part writes the pruned down .xml to a file##
with open('test.xml', 'w') as t:
for line in le.tostring(doc):
t.write(line)
t.close
这个丑陋的解决方案涉及对数据进行大量迭代,我怀疑这远非完成此任务的最有效方法,但它确实有效。
最佳答案
你不是很清楚你到底在追求什么,但这段代码会产生你说你想要的输出:
from lxml import etree as ET
def filter_by_itemid(doc, idlist):
rowset = doc.xpath("/api/result/rowset[@name='assets']")[0]
for elem in rowset.getchildren():
if int(elem.get("itemID")) not in idlist:
rowset.remove(elem)
return doc
doc = ET.parse("test.xml")
filter_by_itemid(doc, [1004072840841])
print(ET.tostring(doc))
关于Python XML 删除一些元素及其子元素但保留特定元素及其子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15168259/
正如标题中所问,我有两个如下结构的 XML 文件 A.xml //here I want to include B.xml
我有一个 xml 文件。根据我的要求,我需要更新空标签,例如我需要更改 to .是否可以像那样更改标签.. 谢谢... 最佳答案 var xmlString=" "; var properStri
我有这样简单的 XML: Song Playing 09:41:18 Frederic Delius Violin Son
在我的工作中,我们有自己的 XML 类来构建 DOM,但我不确定应该如何处理连续的空格? 例如 Hello World 当它被读入 DOM 时,文本节点应该包含 Hello 和 World
我有以下 2 个 xml 文件,我必须通过比较 wd:Task_Name_ID 和 TaskID 的 XML 文件 2。 例如,Main XML File-1 wd:Task_Name_ID 具有以下
我在 Rails 应用程序中有一个 XML View ,需要从另一个文件插入 XML 以进行测试。 我想说“构建器,只需盲目地填充这个字符串,因为它已经是 xml”,但我在文档中看不到这样做的任何内容
我正在重建一些 XML 提要,因此我正在研究何时使用元素以及何时使用带有 XML 的属性。 一些网站说“数据在元素中,元数据在属性中。” 那么,两者有什么区别呢? 让我们以 W3Schools 为例:
在同一个文档中有两个 XML 声明是否是格式正确的 XML? hello 我相信不是,但是我找不到支持我的消息来源。 来自 Extensible Markup Language
我需要在包装器 XML 文档中嵌入任意(语法上有效的)XML 文档。嵌入式文档被视为纯文本,在解析包装文档时不需要可解析。 我知道“CDATA trick”,但如果内部 XML 文档本身包含 CDAT
XML 解析器和 XML 处理器是两个不同的东西吗?他们是两个不同的工作吗? 最佳答案 XML 解析器和 XML 处理器是一样的。它不适用于其他语言。 XML 是通用数据标记语言。解析 XML 文件已
我使用这个 perl 代码从一个文件中读取 XML,然后写入另一个文件(我的完整脚本有添加属性的代码): #!usr/bin/perl -w use strict; use XML::DOM; use
我正在编写一个我了解有限的历史脚本。 对象 A 的类型为 system.xml.xmlelement,我需要将其转换为类型 system.xml.xmldocument 以与对象 B 进行比较(类型
我有以下两个 XML 文件: 文件1 101 102 103 501 502 503
我有以下两个 XML 文件: 文件1 101 102 103 501 502 503
我有一个案例,其中一个 xml 作为输入,另一个 xml 作为输出:我可以选择使用 XSL 和通过 JAXB 进行 Unmarshalling 编码。性能方面,有什么真正的区别吗? 最佳答案 首先,程
我有包含 XML 的 XML,我想使用 JAXB 解析它 qwqweqwezxcasdasd eee 解析器 public static NotificationRequest parse(Strin
xml: mario de2f15d014d40b93578d255e6221fd60 Mario F 23 maria maria
尝试更新 xml 文件数组时出现以下错误。 代码片段: File dir = new File("c:\\XML"); File[] files = dir.listFiles(new Filenam
我怎样才能完成这样的事情: PS /home/nicholas/powershell> PS /home/nicholas/powershell> $date=(Get-Date | ConvertT
我在从 xml 文件中删除节点时遇到一些困难。我发现很多其他人通过各种方式在 powershell 中执行此操作的示例,下面的代码似乎与我见过的许多其他示例相同,但我没有得到所需的行为。 我的目标是将
我是一名优秀的程序员,十分优秀!