[R] saveXML() prefix argument
Earl Brown
ekbrown at k-state.edu
Sat Oct 19 19:51:59 CEST 2013
Duncan, like what happens with Milan, when I print to the console both the accents and the indents come through correctly. However, when I save to a file, the indents disappear:
# when saved to a file the indents disappear
> saveXML(doc, file = "test.xml", encoding = "UTF-8")
# contents of text.xml
<?xml version="1.0" encoding="UTF-8"?>
<tips><tip id="1"><h1>español</h1><p>info about español</p></tip><tip id="2"><h1>português</h1><p>info about português</p></tip></tips>
# correctly prints to console
> saveXML(doc, encoding = "UTF-8")
[1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tips>\n <tip id=\"1\">\n <h1>español</h1>\n <p>info about español</p>\n </tip>\n <tip id=\"2\">\n <h1>português</h1>\n <p>info about português</p>\n </tip>\n</tips>\n"
Here's the info you requested:
> libxmlVersion()
$major
[1] "2"
$minor
[1] "07"
$patch
[1] "03"
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] XML_3.95-0.2
loaded via a namespace (and not attached):
[1] tools_3.0.2
Thanks for the help. Earl Brown
----- Mensaje original -----
De: "Duncan Temple Lang" <dtemplelang at ucdavis.edu>
Para: "Earl Brown" <ekbrown at k-state.edu>
CC: r-help at r-project.org
Enviados: Viernes, 18 de Octubre 2013 19:22:56
Asunto: Re: saveXML() prefix argument
Hi Earl
Unfortunately, the code works for me, i.e. indents _and_ displays the accented vowels correctly.
Can you send me the output of the function call
libxmlVersion()
and also sessionInfo(), please?
D.
On 10/18/13 10:27 AM, Earl Brown wrote:
> Thanks Duncan. However, now I can't get the Spanish and Portuguese accented vowels to come out correctly and still keep the indents in the saved document, even when I set encoding = "UTF-8":
>
> library("XML")
> concepts <- c("español", "português")
> info <- c("info about español", "info about português")
>
> doc <- newXMLDoc()
> root <- newXMLNode("tips", doc = doc)
> for (i in 1:length(concepts)) {
> cur.concept <- concepts[i]
> cur.info <- info[i]
> cur.tip <- newXMLNode("tip", attrs = c(id = i), parent = root)
> newXMLNode("h1", cur.concept, parent = cur.tip)
> newXMLNode("p", cur.info, parent = cur.tip)
> }
>
> # accented vowels don't come through correctly, but the indents are correct:
> saveXML(doc, file = "test1.xml", indent = T)
>
> Resulting file looks like this:
> <?xml version="1.0"?>
> <tips>
> <tip id="1">
> <h1>español</h1>
> <p>info about español</p>
> </tip>
> <tip id="2">
> <h1>português</h1>
> <p>info about português</p>
> </tip>
> </tips>
>
> # accented vowels are correct, but the indents are no longer correct:
> saveXML(doc, file = "test2.xml", indent = T, encoding = "UTF-8")
>
> Resulting file:
> <?xml version="1.0" encoding="UTF-8"?>
> <tips><tip id="1"><h1>español</h1><p>info about español</p></tip><tip id="2"><h1>português</h1><p>info about português</p></tip></tips>
>
> I tried to workaround the problem by simply loading in that resulting file and saving it again:
> doc2 <- xmlInternalTreeParse(file = "test2.xml", asTree = T)
> saveXML(doc2, file = "test_word_around.xml", indent = T)
>
> but still don't get the indents.
>
> Does setting encoding = "UTF-8" override indents = TRUE in saveXML()?
>
> Thanks. Earl
>
>
>
More information about the R-help
mailing list