I'm using the "XML" package and specifically the saveXML() function but I can't get the "prefix" argument of saveXML() to work: library("XML") concepts <- c("one", "two", "three") info <- c("info one", "info two", "info three") root <- newXMLNode("root") 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) } # None of the following output a prefix on the first line of the exported document saveXML(root) saveXML(root, file = "test.xml") saveXML(root, file = "test.xml", prefix = '<?xml version="1.0"?>\n') Am I missing something obvious? Any ideas? Thanks in advance. Earl Brown ----- Earl K. Brown, PhD Assistant Professor of Spanish Linguistics Advisor, TEFL MA Program Department of Modern Languages Kansas State University www-personal.ksu.edu/~ekbrown
Le mercredi 16 octobre 2013 ? 23:45 -0400, Earl Brown a ?crit :> I'm using the "XML" package and specifically the saveXML() function but I can't get the "prefix" argument of saveXML() to work: > > library("XML") > concepts <- c("one", "two", "three") > info <- c("info one", "info two", "info three") > root <- newXMLNode("root") > 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) > } > > # None of the following output a prefix on the first line of the exported document > saveXML(root) > saveXML(root, file = "test.xml") > saveXML(root, file = "test.xml", prefix = '<?xml version="1.0"?>\n') > > Am I missing something obvious? Any ideas?It looks like the function XML:::saveXML.XMLInternalNode() does not use the 'prefix' parameter at all. So it won't be taken into account when calling saveXML() on objects of class XMLInternalNode. I think you should report this to Duncan Temple Lang, as this is probably an oversight. Regards> Thanks in advance. Earl Brown > > ----- > Earl K. Brown, PhD > Assistant Professor of Spanish Linguistics > Advisor, TEFL MA Program > Department of Modern Languages > Kansas State University > www-personal.ksu.edu/~ekbrown > > ______________________________________________ > 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.
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
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 > > >