Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

how to create an R data frame from a xml file

I have a XML Document file. The part of the file looks like this:

-<attr>  
     <attrlabl>COUNTY</attrlabl>  
     <attrdef>County abbreviation</attrdef>  
     <attrtype>Text</attrtype>  
     <attwidth>1</attwidth>  
     <atnumdec>0</atnumdec>  
    -<attrdomv>  
        -<edom>  
            <edomv>C</edomv>  
            <edomvd>Clackamas County</edomvd>  
            <edomvds/>  
         </edom>  
        -<edom>  
            <edomv>M</edomv>  
            <edomvd>Multnomah County</edomvd>  
            <edomvds/>  
         </edom>  
        -<edom>  
            <edomv>W</edomv>  
            <edomvd>Washington County</edomvd>  
            <edomvds/>  
         </edom>  
     </attrdomv>  
 </attr>

From this XML file, I want to create an R data frame with the columns of attrlabl, attrdef, attrtype, and attrdomv. Please note that the attrdomv column should include all of the levels for the category variable. The data frame should look like this:

attrlabl    attrdef                attrtype    attrdomv  
COUNTY      County abbreviation    Text        C Clackamas County; M Multnomah County; W Washington County  

I have an incomplete code like this:

doc <- xmlParse("taxlots.shp.xml")  
dataDictionary <- xmlToDataFrame(getNodeSet(doc,"//attrlabl"))  

Could you please complete my R code? I appreciate any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming this is the correct taxlots.shp.xml file:

<attr>  
     <attrlabl>COUNTY</attrlabl>  
     <attrdef>County abbreviation</attrdef>  
     <attrtype>Text</attrtype>  
     <attwidth>1</attwidth>  
     <atnumdec>0</atnumdec>  
    <attrdomv>  
        <edom>  
            <edomv>C</edomv>  
            <edomvd>Clackamas County</edomvd>  
            <edomvds/>  
         </edom>  
        <edom>  
            <edomv>M</edomv>  
            <edomvd>Multnomah County</edomvd>  
            <edomvds/>  
         </edom>  
        <edom>  
            <edomv>W</edomv>  
            <edomvd>Washington County</edomvd>  
            <edomvds/>  
         </edom>  
     </attrdomv>  
 </attr>

You were almost there:

doc <- xmlParse("taxlots.shp.xml")
xmlToDataFrame(nodes=getNodeSet(doc1,"//attr"))[c("attrlabl","attrdef","attrtype","attrdomv")]
  attrlabl             attrdef attrtype                                             attrdomv
1   COUNTY County abbreviation     Text CClackamas CountyMMultnomah CountyWWashington County

But the last field has not the format you wanted. To do so, require some additional steps:

step1 <- xmlToDataFrame(nodes=getNodeSet(doc1,"//attrdomv/edom"))
step1
  edomv            edomvd edomvds
1     C  Clackamas County        
2     M  Multnomah County        
3     W Washington County  

step2 <- paste(paste(step1$edomv, step1$edomvd, sep=" "), collapse="; ")
step2
[1] "C Clackamas County; M Multnomah County; W Washington County"

cbind(xmlToDataFrame(nodes= getNodeSet(doc1, "//attr"))[c("attrlabl", "attrdef", "attrtype")],
      attrdomv= step2)
  attrlabl             attrdef attrtype                                                      attrdomv
1   COUNTY County abbreviation     Text C Clackamas County; M Multnomah County; W Washington County

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...