2016-09-23 7 views
0

私はXMLファイルを持っています.37個のメジャーフォルダから成っています。各メジャーフォルダにはマイナーフォルダがあり、マイナーフォルダには3個の目印が付きます。XMLファイルのあるノードにノードを挿入するR

このノードを最初のメジャーフォルダにあるこの最初のマイナーフォルダの最初の目印に追加します。

私はそれを実行しようとしましたが、いくつかの問題が は最初、私は

top = newXMLNode("description") 

table = newXMLNode("table", attrs = c(width = 300, border = 1), parent = top) 
tbody <- newXMLNode("tbody",parent = table) 
tr <- newXMLNode("tr",parent = tbody) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = 5,parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 
tr <- newXMLNode("tr",parent = tbody) 

th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = 5,parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 
tr <- newXMLNode("tr",parent = tbody) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 =5,parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 
tbody <- newXMLNode("tbody",parent = table) 

th <- newXMLNode("img",attrs = c(src = URL,width = "700",height= "777",alt=""),parent =top) 

を追加したい、私のノードは、ここに私が試した何がありますです...

data <- xmlTreeParse("xml_data.xml") 
data$doc$children$Folder[[4]][[3]][[3]]$description <- top 
saveXML(data, file ="xml_data.xml") 

最初の問題...私に直面しました私が "data$doc$children$Folder[[4]][[3]][[3]]"をコンソールに書き込むと、コンソールに説明ノードが表示されず、 "data$doc$children$Folder[[4]][[3]][[3]]$description"と表示され、これがなぜ起こるのか分かりません。

第二の問題がある:私は説明が最初の場所マークにかそうでないかどうかを確認するためにsaveXMLを使用するときに....このエラーが表示されます。..

Error in (function (classes, fdef, mtable) : 
    unable to find an inherited method for function ‘saveXML’ for signature ‘"XMLDocument"’ 

私はこの検索と私はdata <- xmlTreeParse(Url,useInternalNodes = TRUE)を使用する必要がありました。 ...これで私には使用できませんdata$doc$children$Folder[[4]][[3]][[3]] ...

ですので、xmlファイルのこの位置にノードを追加する方法はありますか?または私の問題の解決策がありますか?

here's my xmlファイル。

+0

HTMLテーブルをXMLファイルに埋め込むように見えます。要素名のパスではなく、番号付きインデックスを使用しているXMLファイルのどこにアドバイスできますか?[[4] [3] [3]] 'は簡単に見られませんか? – Parfait

+0

私は '[4] [3] [3]' ....を使用しました。この式を使用します。なぜなら、 '$ $ doc $ children $ Folder'の後に$を使うと、他の36個のオプションがあるので、番号4は最初のメジャーフォルダを示し、[[3]]は最初のマイナーフォルダを示し、[3]は最初の目印を示します –

+0

@パフェット、html xmlには、しかし、まだ私のコンピュータ上のファイルにxmlを保存することはできません問題に立ち往生、あなたは私に知っているか教えてください? '$ $ doc $ children $ Folder [[4]] [[3]] [[3]] [[" description "]] < - トップ' –

答えて

1

、その後、新しい子を定義する際に、このノードセットに新しい要素の親を参照してくださいあなたはXMLスニペットを追加する予定の特定の要素を見つけるために、getNodeSetの使用を検討してください:

data <- xmlTreeParse("xml_data.xml") 
firstplacemark <- getNodeSet(data, "/Folder/Folder[1]/Folder[1]/Placemark") 

top <- newXMLNode("description", parent = firstplacemark) 

#... same code as above 


saveXML(data, file="xml_data.xml") 

出力

<?xml version="1.0"?> 
<Folder> 
    <name>All Maps</name> 
    <visibility>0</visibility> 
    <Folder> 
    <name>Kornich ElNile</name> 
    <visibility>0</visibility> 
    <Folder> 
     <name>Kornich ElNile;Zera3y To Arcadia</name> 
     <visibility>0</visibility> 
     <Placemark> 
     <name>Directions from 30.10093 31.2401 to 30.07046 31.22686</name> 
     <visibility>0</visibility> 
     <styleUrl>#msn_ylw-pushpin0</styleUrl> 
     <LineString> 
      <tessellate>1</tessellate> 
      <coordinates> 
       31.23924,30.09923,0 31.23904,30.09886,0 
       31.23859,30.09802,0 31.23836,30.09758,0 31.23813,30.09719,0 
       31.23796000000001,30.09691,0 31.23781,30.09663,0 31.23747,30.09608,0 
       31.2371,30.09546,0 31.2351,30.09159,0 31.23495,30.09131,0 
       31.23487,30.09118,0 31.2347,30.09093,0 31.23448,30.09059,0 
       31.23437,30.09043,0 31.23428,30.09029,0 31.23422,30.09021,0 
       31.23415,30.0901,0 31.23406,30.08992,0 31.23397,30.08968,0 
       31.23387,30.08943,0 31.23373,30.08903,0 31.23359,30.08863,0 
       31.2335,30.08835,0 31.23344,30.08804,0 31.23338,30.08776,0 
       31.23332,30.08751,0 31.23321,30.08723,0 31.23313,30.08706,0 
       31.23306,30.0869,0 31.23295,30.08667,0 31.23293,30.08662,0 
       31.23281,30.08629,0 31.23273,30.08606,0 31.23272,30.086,0 
       31.23266,30.08576,0 31.23259,30.08551,0 31.23245,30.08498,0 
       31.23235,30.08466,0 31.23223,30.08428,0 31.23205,30.08388,0 
       31.23181,30.08342,0 31.23176,30.08333,0 31.23143,30.08276,0 
       31.23135,30.08261,0 31.23119,30.08228,0 31.23109,30.08202,0 
       31.23096,30.08167,0 31.2309,30.08148,0 31.23084,30.08124000000001,0 
       31.2308,30.08102,0 31.23078,30.08076,0 31.23077,30.08049000000001,0 
       31.23076,30.0803,0 31.23073,30.08011,0 31.23068,30.07991,0 
       31.23064,30.07979,0 31.23055,30.07957,0 31.23032,30.07908,0 
       31.23007,30.07857,0 31.23005,30.07854,0 31.22974,30.07796,0 
       31.22963,30.07776,0 31.22945,30.07743,0 31.22907,30.07672,0 
       31.22897,30.07651,0 31.22842,30.07561,0 31.22790000000001,30.07482,0 
       31.22782,30.07468,0 31.22758,30.07423,0 31.22754000000001,30.07415,0 
       31.22754000000001,30.07414,0 31.22751,30.07409,0 31.22744,30.0739,0 
       31.22738,30.07369,0 31.22735,30.07353,0 31.22735,30.07347,0 
       31.22731,30.07324,0 31.22729,30.073,0 31.22727,30.07256,0 
       31.22724,30.07227,0 31.22712,30.07166,0 31.22703,30.07125,0 
       31.22697000000001,30.07097,0      
      </coordinates> 
     </LineString> 
     <description> 
      <table width="300" border="1"> 
      <tbody> 
       <tr> 
       <th scope="col">5</th> 
       <th scope="col">MD</th> 
       <th scope="col">PM</th> 
       </tr> 
       <tr> 
       <th scope="col">5</th> 
       <th scope="col">MD</th> 
       <th scope="col">PM</th> 
       </tr> 
       <tr> 
       <th scope="col">5</th> 
       <th scope="col">MD</th> 
       <th scope="col">PM</th> 
       </tr> 
      </tbody> 
      <tbody/> 
      </table> 
      <img src="http://www.example.com" width="700" height="777" alt=""/> 
     </description> 
     </Placemark> 
... 
関連する問題