[R] saveXML() prefix argument

Earl Brown ekbrown at k-state.edu
Fri Oct 18 19:27:43 CEST 2013


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&#xF1;ol</h1>
    <p>info about espa&#xF1;ol</p>
  </tip>
  <tip id="2">
    <h1>portugu&#xEA;s</h1>
    <p>info about portugu&#xEA;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