[R] read XML

Martin Morgan mtmorgan at fhcrc.org
Wed Jul 30 21:37:08 CEST 2008


"Paulo Cardoso" <pecardoso at netcabo.pt> writes:

> Ok,
>
> I reach this with
> setwd("E:/MBayRNP/output")
> xml.mfd<-xmlTreeParse("myfile.xml")
> str(xml.mfd$doc$children$layout)

I usually parse but not edit XML, so the following might not be the
best. But...

> xml <- xmlRoot(xmlTreeParse("tmp.xml"))
> legend <- xml[["elements"]][["legend"]]
> xml[["elements"]][["legend"]] <- addAttributes(legend, xmax=123)
> cat(saveXML(xml))

Martin    

> and get this:
>
> <layout>
>  <name>Layout 2</name>
>  <pagesByX>1</pagesByX>
>  <pagesByY>1</pagesByY>
>  <elements>
>   <legend refParent="r3" xmax="0.54156171284634758"
> xmin="0.020151133501259435" ymax="0.31480598161051165"
> ymin="0.042461931948864044"/>
>   <component background="auto" border="none" borderDegMinSec="false"
> borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
> borderMargin="12" borderNoOverlaps="true" borderRounding="0" borderStep="1"
> borderUnit="Meter" controlPoints="auto" graticule="auto" grid="auto"
> legend="auto" name="MapMnaziBayhab" northArrow="auto" paging="continuous"
> ref="r1" scaleBar="auto" scope="locked center / scale"
> scopeDetail="646802.61034615175,8856311.5759953968,150"
> xmax="0.35877862595419846" xmin="0.027989821882951665"
> ymax="0.98902384414662947" ymin="0.32371064328183174"/>
>   <component background="auto" border="none" borderDegMinSec="false"
> borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
> borderMargin="12" borderNoOverlaps="true" borderRounding="0" borderStep="1"
> borderUnit="Meter" controlPoints="auto" graticule="auto" grid="auto"
> legend="hide" name="MapMnaziBayall" northArrow="auto" paging="continuous"
> ref="r3" scaleBar="auto" scope="locked center / scale"
> scopeDetail="646802.61034615175,8856311.5759953968,150"
> xmax="0.707379134860051" xmin="0.37659033078880411"
> ymax="0.98902384414662947" ymin="0.32371064328183174"/>
>   <component background="auto" border="none" borderDegMinSec="false"
> borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
> borderMargin="12" borderNoOverlaps="true" borderRounding="0" borderStep="1"
> borderUnit="Degree" controlPoints="auto" graticule="auto" grid="auto"
> legend="auto" name="Map Africa Mnazi" northArrow="auto" paging="continuous"
> ref="r6" scaleBar="auto" scope="entire component" xmax="0.93198992443324935"
> xmin="0.73047858942065491" ymax="0.98902384414662947"
> ymin="0.84662172667648694"/>
>  </elements>
> </layout>
>
> I don't know how to go further and extract elements listed with
> str(xml.mfd$doc$children$layout)
>
> List of 4
>  $ name    :List of 1
>   ..$ text: list()
>   .. ..- attr(*, "class")= chr [1:2] "XMLTextNode" "XMLNode"
>   ..- attr(*, "class")= chr "XMLNode"
>  $ pagesByX:List of 1
>   ..$ text: list()
>   .. ..- attr(*, "class")= chr [1:2] "XMLTextNode" "XMLNode"
>   ..- attr(*, "class")= chr "XMLNode"
>  $ pagesByY:List of 1
>   ..$ text: list()
>   .. ..- attr(*, "class")= chr [1:2] "XMLTextNode" "XMLNode"
>   ..- attr(*, "class")= chr "XMLNode"
>  $ elements:List of 4
>   ..$ legend   : list()
>   .. ..- attr(*, "class")= chr "XMLNode"
>   ..$ component: list()
>   .. ..- attr(*, "class")= chr "XMLNode"
>   ..$ component: list()
>   .. ..- attr(*, "class")= chr "XMLNode"
>   ..$ component: list()
>   .. ..- attr(*, "class")= chr "XMLNode"
>   ..- attr(*, "class")= chr "XMLNode"
>  - attr(*, "class")= chr "XMLNode"
>
> And I need the first two components of elements:List of 4.
> From those components I need to manipulate xmax and xmin and write a new xml
> with these parameters changed only.
>
>>
>> -----Original Message-----
>> From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
>> Sent: quarta-feira, 30 de Julho de 2008 13:49
>> To: Paulo Cardoso
>> Cc: r
>> Subject: Re: [R] read XML
>> 
>> "Paulo Cardoso" <pecardoso at netcabo.pt> writes:
>> 
>> > I have a xml exported by Manifold GIS but I'm not being able to
>> import it
>> > into R using XLM package.
>> 
>> I can
>> 
>> > library(XML)
>> > xml <- xmlTreeParse("tmp.xml", useInternal=TRUE)
>> > xpathApply(xml, "//component/@ymin", xmlValue)
>> [[1]]
>> [1] "0.32371064328183174"
>> 
>> [[2]]
>> [1] "0.32371064328183174"
>> 
>> [[3]]
>> [1] "0.84662172667648694"
>> 
>> so I wonder what specifically your difficulty is?
>> 
>> Best,
>> 
>> Martin
>> 
>> >
>> >
>> >
>> > The file have this structure:
>> >
>> >
>> >
>> >   <?xml version="1.0" encoding="UTF-8" ?>
>> >
>> > - <layout>
>> >
>> >   <name>Layout 2</name>
>> >
>> >   <pagesByX>1</pagesByX>
>> >
>> >   <pagesByY>1</pagesByY>
>> >
>> > - <elements>
>> >
>> >   <legend refParent="r3" xmax="0.54156171284634758"
>> > xmin="0.020151133501259435" ymax="0.31480598161051165"
>> > ymin="0.042461931948864044" />
>> >
>> >   <component background="auto" border="none" borderDegMinSec="false"
>> > borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
>> > borderMargin="12" borderNoOverlaps="true" borderRounding="0"
>> borderStep="1"
>> > borderUnit="Meter" controlPoints="auto" graticule="auto" grid="auto"
>> > legend="auto" name="MapMnaziBayhab" northArrow="auto"
>> paging="continuous"
>> > ref="r1" scaleBar="auto" scope="locked center / scale"
>> > scopeDetail="646802.61034615175,8856311.5759953968,150"
>> > xmax="0.35877862595419846" xmin="0.027989821882951665"
>> > ymax="0.98902384414662947" ymin="0.32371064328183174" />
>> >
>> >   <component background="auto" border="none" borderDegMinSec="false"
>> > borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
>> > borderMargin="12" borderNoOverlaps="true" borderRounding="0"
>> borderStep="1"
>> > borderUnit="Meter" controlPoints="auto" graticule="auto" grid="auto"
>> > legend="hide" name="MapMnaziBayall" northArrow="auto"
>> paging="continuous"
>> > ref="r3" scaleBar="auto" scope="locked center / scale"
>> > scopeDetail="646802.61034615175,8856311.5759953968,150"
>> > xmax="0.707379134860051" xmin="0.37659033078880411"
>> > ymax="0.98902384414662947" ymin="0.32371064328183174" />
>> >
>> >   <component background="auto" border="none" borderDegMinSec="false"
>> > borderEachPage="true" borderFont="Tahoma, 8" borderFore="#000000"
>> > borderMargin="12" borderNoOverlaps="true" borderRounding="0"
>> borderStep="1"
>> > borderUnit="Degree" controlPoints="auto" graticule="auto" grid="auto"
>> > legend="auto" name="Map Africa Mnazi" northArrow="auto"
>> paging="continuous"
>> > ref="r6" scaleBar="auto" scope="entire component"
>> xmax="0.93198992443324935"
>> > xmin="0.73047858942065491" ymax="0.98902384414662947"
>> > ymin="0.84662172667648694" />
>> >
>> >   </elements>
>> >
>> >   </layout>
>> >
>> >
>> >
>> > Any idea?
>> >
>> >
>> >
>> > Thanks.
>> >
>> >
>> >
>> > Paulo Cardoso
>> >
>> >
>> > 	[[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> 
>> --
>> Martin Morgan
>> Computational Biology / Fred Hutchinson Cancer Research Center
>> 1100 Fairview Ave. N.
>> PO Box 19024 Seattle, WA 98109
>> 
>> Location: Arnold Building M2 B169
>> Phone: (206) 667-2793
>> No virus found in this incoming message.
>> Checked by AVG.
>> Version: 8.0.100 / Virus Database: 270.5.6/1579 - Release Date: 29-07-
>> 2008 6:43
>

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list