XML解析器遍历包含XML树的文本文档,并允许使用层次结构中的信息。将此标记用于实现XML解析器的问题,或者通过使用给定语言的现有解析器生成的问题。
如何在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 文件,您的代码就会失败。
我正在尝试将 xml 转换为 json,但在 xml 到 json 转换期间未形成多个行项目。我们总是在 xml 消息中包含单个或多个 Lineitem,其名称与“vpoItemLi...
.Net Maui date xml 反序列化在 Android 中失败,但在 Windows 中工作
我有一个 .Net Maui Hybrid 项目,它调用返回 XML 的 API。 API 返回 xml,并使用以下代码反序列化为对象: var 序列化器 = new XmlSerializer(typeof(List<
尝试使用 WebClient 从 Spring Boot 调用带有 XML 响应的 API 时出现 UnmarshalException
我正在尝试在 Spring Boot 应用程序中使用 WebClient 调用返回 XML 响应的 API。我确信我收到了 API 调用的 200,但代码无法将响应解析为
我有一个自定义 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
我有一个简单的要求,我需要获取属性 xml:id 的值,即 af1。 我正在使用 SAXParser,这是我的 xpath:a/aff/@xml:id 相反,我能够获取 val...
java.lang.NoSuchMethodError:org.springframework.util.ReflectionUtils.clearCache()错误
我是 Spring 新手,我正在尝试使用 Spring 容器构造对象,该容器将解析我的 XML 文件,所以我有一个 .xml 和两个 .java 类。 春季版本5.1.0 我正在研究 si...
为什么 Firefox 会抛出 XML 格式错误,而 Chrome 不会?
Firefox 对我的 XML 字符串抛出格式错误,而 Chrome 可以毫无问题地解析它。 我传递了一个 XML 字符串(在变量“doc”中): var xmlDoc = parseXML(doc); 这在 Chrome 中有效,但...
我正在尝试循环下面的 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 }); }
我正在尝试修复包含数千行错误的 XML 文件: 开始和结束标记不匹配错误 我现在正在使用 simpleXML 来解析这个文件,所以在用这个库解析之前......
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:
我想要这个输出(示例): 我要这个 我正在处理 XML/TEI 文档,我需要使用 XPath 表达式,并且我希望将 div/u 中的文本作为输出,但没有节点 e 内的文本...
将 XML 序列化为 POCO,然后序列化为 JSON 字符串
我有以下 XML,我需要将其序列化为 POCO,然后将其反序列化为 JSON。 这是 XML:
有 micropython 的 XML 解析器库吗?我想解析文本文件或字符串中的 XML 文件。这是针对 Pico 的,我使用的是 Thonny。谢谢。
使用 apache-tika 和 python 从 PDF 中提取内联图像
我需要将 PDF 文档转换为 XML 或 JSON 格式,包括内嵌图像。 到目前为止,我可以使用 python-tika 库生成 XML。 要重现,请使用带有内嵌图像的测试 PDF 文档,...
文档文档 = null; DocumentBuilder解析器 = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 文档 = parser.paser(xmlFilePath); 解析异常 萨克斯异常 IO异常 回来了
我的同事为有效负载中的每个元素提供了一个命名空间 ns0 以实现最佳实践,但我的解析器停止了工作。阅读有关该主题的更多内容,似乎声明每个元素的名称空间是......
我有一个像这样的XML: ... 我有一个像这样的XML: <?xml version="1.0" encoding="UTF-8"?> <TEI xmlns="http://www.tei-c.org/ns/1.0"> <teiHeader> ... </teiHeader> <text> <body> <head rend="Body A">DOCUMENT_TITLE</head> <div rend="entry"> <head rend="time">TIME_1</head> <p rend="Body A"> INFORMATION A</p> <p rend="content"> <hi rend="italic"> CONTENT1 </hi> </p> </div> <div rend="entry"> <head rend="time">TIME_2</head> <p rend="Body A"> INFORMATION A</p> <p rend="Body A"> INFORMATION A</p> </div> <div rend="entry"> <head rend="time">TIME_3</head> <p rend="Body A"> INFORMATION A</p> <p rend="content"> <hi rend="italic"> CONTENT3 </hi> </p> <div rend="entry"> <p rend="Body A"> INFORMATION A</p> <p rend="content"> <hi rend="italic"> CONTENT4 </hi> </p> </div> </body> </text> </TEI> ...有许多缺少的参数,但我想获得一个 data.frame,其中每个“div”都有一行,如下所示: div 时间 内容 1 时间1 内容1 2 时间2 不适用 3 时间3 内容3 4 不适用 内容4 当参数缺失时,使用 NA。 我尝试这样的方法 data_xml <- read_xml(xmlfile) div <-xml_find_all(data_xml, xpath = ".//div") df <- tibble::tibble( date = div %>% xml_text(), content = div %>% xml_find_first('./p[@rend="content"/hi[@rend="italic"]]') %>% xml_text() ) 但是 xml_find_all 确实返回一个空列表。 根据一些建议,我尝试这种方式,实际工作 doc <- htmlParse(xmlfile) div <- getNodeSet(doc, '//div') dates<- xpathSApply(doc,'//div/text()',xmlValue) abstracts<-unlist(xpathSApply(doc,'//p[@rend="content"]//hi[@rend="italic"]',xmlValue)) 我正确地获得了我想要的字符串,但我失去了对应性,因为许多 div 没有内容或没有时间信息的头部(意味着 div、日期、摘要有不同的长度)。有什么建议么? TIA 显示的输入格式错误,因此 read_xml 会给出错误。由于问题表明它有效,因此在将 XML 移动到问题时一定存在转录错误。我们在最后的注释中的第 4 个之前添加了一个。 由于 XML 使用命名空间,因此首先使用 xml_ns_strip 去除该命名空间以避免出现问题。然后形成适当的 xpath 表达式,生成所需的节点,并将其转换为变量 中的 dcf 格式(这是一种名称:值格式,其中每个字段位于单独的行上,并且空行分隔记录 - 有关详细信息,请参阅 ?read.dcf) dcf。使用 read.dcf 读取该内容,将生成的字符矩阵转换为数据帧,修复类型并将 div 列转换为序列号。 library(dplyr) library (xml2) doc <- read_xml(Lines) # see Note at end for Lines nodes <- doc %>% xml_ns_strip() %>% xml_find_all('//div | //head[@rend="time"] | //hi[@rend="italic"]') dcf <- sapply(nodes, function(x) if (xml_name(x) == "div") "\ndiv:1" else paste0(xml_attr(x, "rend"), ":", xml_text(x)) ) dcf dcf %>% textConnection() %>% read.dcf() %>% as.data.frame() %>% type.convert(as.is = TRUE) %>% setNames(c("div", "time", "content")) %>% mutate(div = cumsum(div)) 给予 div time content 1 1 TIME_1 CONTENT1 2 2 TIME_2 <NA> 3 3 TIME_3 CONTENT3 4 4 <NA> CONTENT4
使用 Python requests.post() 发送 XML Soap 请求
我确信我这样做是错误的,因为我对 Xml 或 SOAP 一无所知,但我正在尝试使用 Python requests.post() 发送 SOAP 请求。我想我可以把身体作为
我正在尝试使用一些 XML 文件来进行句子标记,同时保持文件的原始结构。这些文件看起来像这样: ...