xml - 在 R 中解析 xml 文件及其元素

标签 xml r

我正在尝试解析具有以下结构的 XML 文件

 <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
            <xs:complexType name="Context">
                <xs:sequence>
                    <xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>

我尝试选择具有 name="Context" 的节点标签,

在此标记中选择标记为 ID 的元素,
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>

并将值“111111”添加到此元素 ID

任何关于完成此任务的指示/答案都会非常有帮助。提前致谢。

最佳答案

你可以做

txt <- '<xs:schema attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
            <xs:complexType name="Context">
                <xs:sequence>
                    <xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
                    <xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>'
library(XML)
xml <- xmlParse(txt, asText=TRUE)
ns <- getNodeSet(xml, '//*[@name="Context"]/xs:sequence/xs:element')
id <- which(sapply(ns, xmlGetAttr, "name") == "ID")
xmlValue(ns[[id]]) <- "11111"
xml
# <?xml version="1.0"?>
# <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
#   <xs:complexType name="Context">
#     <xs:sequence>
#       <xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="ID" nillable="true" type="xs:string">11111</xs:element>
#       <xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
#       <xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
#     </xs:sequence>
#   </xs:complexType>
# </xs:schema>

关于xml - 在 R 中解析 xml 文件及其元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095844/

相关文章:

xml - 是否有一个 cordova 插件可以从 config.xml 中读取值?

html - 使用 namespace 扩展 XHTML

r - 插入符号神经网络错误 : "missing values in resampled performance measures"

javascript - 将 onclick 打开超链接事件添加到在 R 中创建的 html 小部件

xml - 如何确保 XML Schema 序列至少包含一个元素

android - 自定义方形线性布局。如何?

c# - 如何更改 XML 属性

R Shiny,如何使数据表对数据表中的复选框使用react

css - 是否可以将 ShinyWidgets 中 progressBar() 的颜色更改为自定义颜色?

r - 从ggplot2创建的nls拟合中提取系数