xml-parsing 相关问题

XML解析器遍历包含XML树的文本文档,并允许使用层次结构中的信息。将此标记用于实现XML解析器的问题,或者通过使用给定语言的现有解析器生成的问题。

如何用 Ansible 替换 XML 元素的文本?

下面的 XML 文件列出了 JAR 及其版本: 公共/foo-1.1.0.jar 下面的 XML 文件列出了 JAR 及其版本: <?xml version='1.0'?> <Files> <!-- ... --> <File Check="Y">public/foo-1.1.0.jar</File> <File Check="Y">public/bar-2.3.11.jar</File> </Files> 我需要将foo更新到版本1.2.1。我想使用 Ansible xml-module ——但我无法弄清楚语法,所以它会: 替换foo的任何现有条目(如果它引用不同版本)。 添加 foo 的新条目(如果不存在)。 可选:只为foo留下一个条目——具有指定的版本——即使之前有重复的条目(例如之前失败的升级尝试)。 我想到的唯一方法是使用两个任务:首先删除任何现有条目 (state=absent),然后创建一个新条目。 任何人都可以建议,如何一次性完成同样的事情? 尝试 powershell 脚本 using assembly System.Xml.Linq $input_filename = 'c:\temp\test.xml' $output_filename = 'c:\temp\test1.xml' $doc = [System.Xml.Linq.XDocument]::Load($input_filename) $files = $doc.Descendants('Files') $foos = [System.Linq.Enumerable]::Where($files, [Func[object,bool]]{ param($x) [string]$x.Value.StartsWith('public/foo')}) $pattern = '(public/foo-)(.*)(.jar)' $newVersion = '1.2.1' foreach($foo in $foos) { $version = $foo[0].Value $newValue = $version -replace $pattern, '$1xyz$3' $newValue = $newValue -replace 'xyz', "$newVersion" $foo.FirstNode.SetValue($newValue) } $doc.Save($output_filename)

回答 1 投票 0

lxml 获取没有标签的元素的文本

我正在使用 lxml 库和 python 来解析一个简单的 XML,该 XML 在本例中打印下一个元素的文本 HD,如下面的 XML 所示 有史以来最好的书...

回答 1 投票 0

使用 shell 脚本在 xml 文件中添加 xml 元素

我有一个xml文件,我需要在其中添加数据,我尝试使用sed。谁能帮我解决我做错的地方。 我有一个像这样的 xml 文件: 我有一个 xml 文件,我需要在其中添加数据,我尝试使用 sed。谁能帮我解决我做错的地方。 我有一个像这样的xml文件: <!DOCTYPE sf-migration PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN" "/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd"> <sf-migration toVersion="6.1.0.0" fromVersion="5.0.0.0"> <users></users> </sf-migration> 现在我需要在用户中添加以下文件: <sfuser xid="user1000"> <username>beq03838</username> <email>[email protected]</email> <alternateEmail1></alternateEmail1> <dateCreated>2016-12-20 14:02:23 CET</dateCreated> <dateLastModified>2016-12-20 14:02:23 CET</dateLastModified> <detail></detail> <monitoringPreference>email</monitoringPreference> <lastLogin></lastLogin> </sfuser> 我的输出应该如下: enter <!DOCTYPE sf-migration PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN" "/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd"> <sf-migration toVersion="6.1.0.0" fromVersion="5.0.0.0"> <users> <sfuser xid="user1000"> <username>beq03838</username> <email>[email protected]</email> <alternateEmail1></alternateEmail1> <dateCreated>2016-12-20 14:02:23 CET</dateCreated> <dateLastModified>2016-12-20 14:02:23 CET</dateLastModified> <lastLogin></lastLogin> </sfuser> </users> </sf-migration> 我使用下面的 shell 脚本来附加文本: CONTENT=give the suer content C=$(echo $CONTENT | sed 's/\//\\\//g') sed "/<\/users>/ s/.*/${C}\n&/" /tmptest/user1.xml 我做错了什么?还有其他简单的方法可以在 xml 中添加元素吗? 我需要在 shell 中完成此操作,因为我的完整代码在 shell 中 使用多个文件并在命令行上转义是非常容易出错和 hacky 的。在大多数语言中,您可以使用非常小的脚本来实现相同的目的。这是 Python 2 中的示例: template = open("user.xml").read() snippet = open("user_data.xml").read() result = template.replace("</users>", snippet + "</users>") open("user.xml", "w").write(result) 输出: <!DOCTYPE sf-migration PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN" "/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd"> <sf-migration toVersion="6.1.0.0" fromVersion="5.0.0.0"> <users> <sfuser xid="user1000"> <username>beq03838</username> <email>[email protected]</email> <alternateEmail1></alternateEmail1> <dateCreated>2016-12-20 14:02:23 CET</dateCreated> <dateLastModified>2016-12-20 14:02:23 CET</dateLastModified> <detail></detail> <monitoringPreference>email</monitoringPreference> <lastLogin></lastLogin> </sfuser> </users> 如果您需要更频繁地执行此操作,请使用此脚本: #!/usr/bin/python import sys if len(sys.argv) != 4: print "This script prepends the given pattern in a template with a snippet. Both snippet and template are files." print "Usage:" print sys.argv[0] + " template snippet replacementpattern" sys.exit(1) template = open(sys.argv[1]).read() snippet = open(sys.argv[2]).read() pattern = sys.argv[3] if not pattern in template: print "Template doesn't contain the pattern." sys.exit(2) print template.replace(pattern, snippet + pattern) 用途: ./prepend.py template.xml snippet.xml "</users>"

回答 1 投票 0

如何确定 Apache Spark 和 scala 中的无效 XML 字符串

我有一个用户定义的函数,如下所示 案例类主体结果(文本:字符串,代码:字符串) val bodyudf = udf{ (body: 字符串) => //将 body 标签显式附加到

回答 2 投票 0

如何在XML标签之间添加一个字符串,该字符串的值是从文本文件获取的? (重击)

我有一些 XML 标签名称存储在一个文本文件中,这是一个 XML 文件,我想在其中添加相同的字符串作为这些标签的另一个值。 XML 标签文件: 姓名1 姓名2 姓名3 XML 文件: 我有一些 XML 标签名称存储在一个文本文件中,这是一个 XML 文件,我想在其中添加相同的字符串作为这些标签的另一个值。 XML 标签文件: name1 name2 name3 XML 文件: <Category name="name1">value1 value2 value3 ...</Category> <Category name="name2">value1 value2 value3 ...</Category> <Category name="name3">value1 value2 value3 ...</Category> 我尝试了几十个“sed”、“cat”命令,但非常痛苦......我期望在使用脚本/命令后,XML 文件会如下所示: <Category name="name1">VALUE4 value1 value2 value3 ...</Category> <Category name="name2">VALUE4 value1 value2 value3 ...</Category> <Category name="name3">VALUE4 value1 value2 value3 ...</Category> (VALUE4是我要添加的字符串,我想将它添加为第一个值!) 提前感谢您的帮助! 勒普松 这项工作的工具是 XSLT。 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSLTransform" version="3.0" expand-text="yes"/> <xsl:variable name="tags" select="tokenize(unparsed-text('tags-file.txt'))"/> <xsl:mode on-no-match="shallow-copy"/> <xsl:template match="Category[@name=$tags]/text()"> <xsl:text>VALUE4 {.}</xsl:text> </xsl:template> </xsl:transform> 我在这里使用了XSLT 3.0。 1.0 处理器的使用范围更广,但这使得代码更加冗长。 不要尝试使用基于文本的工具来执行此操作。正如您所发现的,正确执行它是一件很痛苦的事情,当您成功时,只要有人提供与您测试的文件稍有不同的 XML 文件,您的代码就会失败。

回答 1 投票 0

具有多个 LineItems 的 Xml 到 Json

我正在尝试将 xml 转换为 json,但在 xml 到 json 转换期间未形成多个行项目。我们总是在 xml 消息中包含单个或多个 Lineitem,其名称与“vpoItemLi...

回答 1 投票 0

.Net Maui date xml 反序列化在 Android 中失败,但在 Windows 中工作

我有一个 .Net Maui Hybrid 项目,它调用返回 XML 的 API。 API 返回 xml,并使用以下代码反序列化为对象: var 序列化器 = new XmlSerializer(typeof(List<

回答 1 投票 0

尝试使用 WebClient 从 Spring Boot 调用带有 XML 响应的 API 时出现 UnmarshalException

我正在尝试在 Spring Boot 应用程序中使用 WebClient 调用返回 XML 响应的 API。我确信我收到了 API 调用的 200,但代码无法将响应解析为

回答 1 投票 0

使用 python 将结构化 xml 转换为表

我有一个自定义 xml 文件,我应该在其中仅获取有关实体及其相应属性的详细信息,并将其转换为表格格式。以下是 xml 文件的示例。 我有一个自定义 xml 文件,我应该在其中仅获取有关实体及其相应属性的详细信息,并将其转换为表格格式。这是 xml 文件的示例。 <ESpace Key="ESpace:y7vmLUGpAMoLhw4MvpnAPg" Name="Onboarding_Core" UserProviderEspace="Users" DefaultThemeNewRuntime="ThemeReference:rQ3WUJw0o0+BC9ZDjXD7uA" DefaultTransition="Fade" UseCookies="Yes" WebScreenRenderingMode="HTML5" ModuleType="CrossDevice"> <Actions> </Actions> <Entities> <Entity Key="Entity:X2oBACCeRUeSQxJFoyn1hA" ExposeReadOnly="Yes" Identifier="Attribute:_EXIewz0o024UgjslFt7_g" Public="Yes" Name="Document"> <Attributes> <Attribute Key="Attribute:_EXIewz0o024UgjslFt7_g" IsMandatory="Yes" Name="Id" DataType="Long Integer" IsAutoNumber="Yes"/> <Attribute Key="Attribute:ypRA49SJBkexc+ICR12zdw" Length="50" Name="Name" DataType="Text"/> <Attribute Key="Attribute:mdWB6OaTkE26BwxQRRfbBg" Name="CreatedOn" DataType="Date Time"/> <Attribute Key="Attribute:W0foZDFa5kme6QySnTNEMA" Name="Binary" DataType="Binary Data"/> </Attributes> </Entity> <Entity Key="Entity:6NuECH7xe06M250tQsPsJA" IsStaticEntity="Yes" IsMultitenant="No" ExposeReadOnly="Yes" Identifier="Attribute:jBExnTjCy0GOtV5fZZG0pw" Public="Yes" Name="DateFormat" Folder="Folder:SErnhCWKOUKpp39VDQVVcg"> <Attributes> <Attribute Key="Attribute:jBExnTjCy0GOtV5fZZG0pw" IsMandatory="Yes" Name="Id" DataType="Integer" IsAutoNumber="Yes" DeleteRule="Ignore"/> <Attribute Key="Attribute:Q0S8yDbafEi+YMF0rXnbvg" IsMandatory="Yes" Length="50" Name="Format" DataType="Text" DeleteRule="Ignore"/> <Attribute Key="Attribute:XUw_zMWoZki2qWuZcYRizw" IsMandatory="Yes" Length="50" Name="Example" DataType="Text" DeleteRule="Ignore"/> <Attribute Key="Attribute:YpcYFT5YNUKxqaYLZOe6qA" IsMandatory="Yes" Name="Order" DataType="Integer" DeleteRule="Ignore"/> <Attribute Key="Attribute:3QRHk0Wl7kuwg4gmzIs8yw" IsMandatory="Yes" Name="IsActive" DataType="Boolean" DeleteRule="Ignore"/> </Attributes> </Entity> 总共有 28 个实体(表),每个实体都有自己的一组属性(列)。我想以表格格式显示所有 28 个实体的每个实体及其相应的属性。 我是Python的初学者。我尝试使用下面的 python 代码来获取第一个实体名称并将其相应的列/属性存储到字典中,但发生的情况是属性的 root.findall 获取 xml 文件中存在的所有属性,而不仅仅是与第一个实体相对应的属性实体。如何分别获取每个实体的属性并将其转换为表格格式?共有28个实体。 table_list=[] Attribute_list=[] for entity_list in root.findall('.//Entity'): entity_dict={} entity_dict['Entity_Name']=entity_list.get('Name') print(entity_dict) for attribute_list in root.findall('.//Attribute'): attribute_dict={} attribute_dict['Attribute_Name']=attribute_list.get('Name') for child_elem_1 in attribute_list: attribute_dict[child_elem_1.tag]=child_elem_1.text Attribute_list.append(attribute_dict) print(Attribute_list) df= pd.DataFrame(Attribute_list) print(df.head(10)) 这是一个如何使用 beautifulsoup 从 XML 片段创建数据框的示例: import pandas as pd from bs4 import BeautifulSoup xml_doc = """\ <ESpace Key="ESpace:y7vmLUGpAMoLhw4MvpnAPg" Name="Onboarding_Core" UserProviderEspace="Users" DefaultThemeNewRuntime="ThemeReference:rQ3WUJw0o0+BC9ZDjXD7uA" DefaultTransition="Fade" UseCookies="Yes" WebScreenRenderingMode="HTML5" ModuleType="CrossDevice"> <Actions> </Actions> <Entities> <Entity Key="Entity:X2oBACCeRUeSQxJFoyn1hA" ExposeReadOnly="Yes" Identifier="Attribute:_EXIewz0o024UgjslFt7_g" Public="Yes" Name="Document"> <Attributes> <Attribute Key="Attribute:_EXIewz0o024UgjslFt7_g" IsMandatory="Yes" Name="Id" DataType="Long Integer" IsAutoNumber="Yes"/> <Attribute Key="Attribute:ypRA49SJBkexc+ICR12zdw" Length="50" Name="Name" DataType="Text"/> <Attribute Key="Attribute:mdWB6OaTkE26BwxQRRfbBg" Name="CreatedOn" DataType="Date Time"/> <Attribute Key="Attribute:W0foZDFa5kme6QySnTNEMA" Name="Binary" DataType="Binary Data"/> </Attributes> </Entity> <Entity Key="Entity:6NuECH7xe06M250tQsPsJA" IsStaticEntity="Yes" IsMultitenant="No" ExposeReadOnly="Yes" Identifier="Attribute:jBExnTjCy0GOtV5fZZG0pw" Public="Yes" Name="DateFormat" Folder="Folder:SErnhCWKOUKpp39VDQVVcg"> <Attributes> <Attribute Key="Attribute:jBExnTjCy0GOtV5fZZG0pw" IsMandatory="Yes" Name="Id" DataType="Integer" IsAutoNumber="Yes" DeleteRule="Ignore"/> <Attribute Key="Attribute:Q0S8yDbafEi+YMF0rXnbvg" IsMandatory="Yes" Length="50" Name="Format" DataType="Text" DeleteRule="Ignore"/> <Attribute Key="Attribute:XUw_zMWoZki2qWuZcYRizw" IsMandatory="Yes" Length="50" Name="Example" DataType="Text" DeleteRule="Ignore"/> <Attribute Key="Attribute:YpcYFT5YNUKxqaYLZOe6qA" IsMandatory="Yes" Name="Order" DataType="Integer" DeleteRule="Ignore"/> <Attribute Key="Attribute:3QRHk0Wl7kuwg4gmzIs8yw" IsMandatory="Yes" Name="IsActive" DataType="Boolean" DeleteRule="Ignore"/> </Attributes> </Entity> </Entities> </ESpace> """ soup = BeautifulSoup(xml_doc, "xml") out = [] for e in soup.select("Entity"): attrs = {} for a in e.select("Attribute"): out.append({"Entity Name": e["Name"], **a.attrs}) df = pd.DataFrame(out) print(df) 打印: Entity Name Key IsMandatory Name DataType IsAutoNumber Length DeleteRule 0 Document Attribute:_EXIewz0o024UgjslFt7_g Yes Id Long Integer Yes NaN NaN 1 Document Attribute:ypRA49SJBkexc+ICR12zdw NaN Name Text NaN 50 NaN 2 Document Attribute:mdWB6OaTkE26BwxQRRfbBg NaN CreatedOn Date Time NaN NaN NaN 3 Document Attribute:W0foZDFa5kme6QySnTNEMA NaN Binary Binary Data NaN NaN NaN 4 DateFormat Attribute:jBExnTjCy0GOtV5fZZG0pw Yes Id Integer Yes NaN Ignore 5 DateFormat Attribute:Q0S8yDbafEi+YMF0rXnbvg Yes Format Text NaN 50 Ignore 6 DateFormat Attribute:XUw_zMWoZki2qWuZcYRizw Yes Example Text NaN 50 Ignore 7 DateFormat Attribute:YpcYFT5YNUKxqaYLZOe6qA Yes Order Integer NaN NaN Ignore 8 DateFormat Attribute:3QRHk0Wl7kuwg4gmzIs8yw Yes IsActive Boolean NaN NaN Ignore

回答 1 投票 0

如何使用 xpath 检索属性名称中包含冒号的属性值

我有一个简单的要求,我需要获取属性 xml:id 的值,即 af1。 我正在使用 SAXParser,这是我的 xpath:a/aff/@xml:id 相反,我能够获取 val...

回答 3 投票 0

java.lang.NoSuchMethodError:org.springframework.util.ReflectionUtils.clearCache()错误

我是 Spring 新手,我正在尝试使用 Spring 容器构造对象,该容器将解析我的 XML 文件,所以我有一个 .xml 和两个 .java 类。 春季版本5.1.0 我正在研究 si...

回答 1 投票 0

为什么 Firefox 会抛出 XML 格式错误,而 Chrome 不会?

Firefox 对我的 XML 字符串抛出格式错误,而 Chrome 可以毫无问题地解析它。 我传递了一个 XML 字符串(在变量“doc”中): var xmlDoc = parseXML(doc); 这在 Chrome 中有效,但...

回答 1 投票 0

如何使用c#循环遍历xml文档节点以使用xpath存储值

我正在尝试循环下面的 xml 文档 16 我正在尝试循环下面的 xml 文档 <proposer driverId="1"> <conviction convictionId="1"> <code>16</code> </conviction> <conviction convictionId="2"> <code>10</code> </conviction> </proposer> <driver driverId="2"> <conviction convictionId="3"> <code>22</code> </conviction> </driver> 我的想法是获取所有信念代码,无论是提议者还是驱动程序,并将其存储在字符串列表中,然后通过发送索引,我执行 elementAtDefault 来获取代码 DrivingConvictionCode1 = GetConvictionCode(1, risk), DrivingConvictionCode2 = GetConvictionCode(2, risk), DrivingConvictionCode3 = GetConvictionCode(3, risk), 在 GetConvictionCode 方法中,我将索引值与 XmlDoc 一起传递,并调用 GetConvictions 方法,然后执行 Elementatdefault 以基于索引获取 ConvictionCode private string GetConvictionCode(int convictionSrNo, XmlDocument risk) { var convictions = GetConvictions(risk); return convictions.ElementAtOrDefault(convictionSrNo - 1) == null ? string.Empty : convictions[convictionSrNo - 1]; } private List<string> GetConvictions(XmlDocument risk) { var convictions = new List<string>(); foreach (XmlNode selectNode in risk.SelectNodes("/risk/proposer/conviction")) { convictions.AddRange(new[] {selectNode.SelectSingleNode("/risk/proposer/conviction/code").InnerText}); } foreach (XmlNode selectNode in risk.SelectNodes("/risk/driver/conviction")) { convictions.AddRange(new[] {selectNode.SelectSingleNode("/risk/driver/conviction/code").InnerText}); } return convictions; } 我在这里面临的问题是在循环中下一个信念值正在被旧的值取代,所以如果第一次循环它是 16 下一次它应该是 10 但它来了 16。任何人都可以帮忙或告诉我任何其他方式 我期望的输出是 DrivingConvictionCode1 = GetConvictionCode(1, risk) > 16, DrivingConvictionCode2 = GetConvictionCode(2, risk) > 10, DrivingConvictionCode3 = GetConvictionCode(3, risk) > 22, 您应该使用 InnerText 而不是 .SelectSingleNode("<path>").InnerText 来获取值。 foreach (XmlNode selectNode in risk.SelectNodes("/risk/proposer/conviction")) { convictions.AddRange(new[] { selectNode.InnerText }); } foreach (XmlNode selectNode in risk.SelectNodes("/risk/driver/conviction")) { convictions.AddRange(new[] { selectNode.InnerText }); }

回答 1 投票 0

如何修复 XML“开始和结束标记不匹配错误”?

我正在尝试修复包含数千行错误的 XML 文件: 开始和结束标记不匹配错误 我现在正在使用 simpleXML 来解析这个文件,所以在用这个库解析之前......

回答 3 投票 0

Gradle SAXParseException cvc-complex-type.2.4.a

当我从 jcenter() 更改为 mavenCentral() 时。在 GitHub CI 上执行 ./gradlew assembleDebug --stacktrace --debug 时,我遇到了以下错误: 2024-03-30T14:05:43.0666766Z cvc-complex-type.2.4.a:

回答 1 投票 0

XPath - 如何从子节点中排除文本

我想要这个输出(示例): 我要这个 我正在处理 XML/TEI 文档,我需要使用 XPath 表达式,并且我希望将 div/u 中的文本作为输出,但没有节点 e 内的文本...

回答 2 投票 0

将 XML 序列化为 POCO,然后序列化为 JSON 字符串

我有以下 XML,我需要将其序列化为 POCO,然后将其反序列化为 JSON。 这是 XML:

回答 1 投票 0

有 micropython 的 XML 解析器库吗?

有 micropython 的 XML 解析器库吗?我想解析文本文件或字符串中的 XML 文件。这是针对 Pico 的,我使用的是 Thonny。谢谢。

回答 1 投票 0

使用 apache-tika 和 python 从 PDF 中提取内联图像

我需要将 PDF 文档转换为 XML 或 JSON 格式,包括内嵌图像。 到目前为止,我可以使用 python-tika 库生成 XML。 要重现,请使用带有内嵌图像的测试 PDF 文档,...

回答 3 投票 0

找不到元素-SOAP的声明

文档文档 = null; DocumentBuilder解析器 = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 文档 = parser.paser(xmlFilePath); 解析异常 萨克斯异常 IO异常 回来了

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.