2016-05-08 52 views
2

我試圖解析具有以下結構解析XML文件和其中的R

<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"標籤的節點的XML文件該標籤內的元件

選擇元素標記爲ID,
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>

並將值「111111」添加到此元素ID

完成此任何指針/答案將非常有幫助。提前致謝。

+0

的'xml2'包擁有所有你需要的解析,但寫你需要潛入'XML'包,這是一種巨大的。 – alistaire

回答

2

你可以做

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> 
+0

太棒了!我很難在'getNodeSet'函數中傳遞正確的參數。太棒了。非常感謝你 –