I have one input xml file.
cat sample.xml
<Text>
<p>ABC </p>
</Text>
R script
library(XML)
doc = xmlTreeParse("sample.xml", useInternal = TRUE)
top<-xmlRoot(doc)
sub("<","<",top[[1]])
How can i fix above pblm?
Error Message: Error in as.vector(x, "character") : cannot coerce type 'externalptr' to vector of type 'character'
Edit: Aim is to use readHTMLTable() function for particular node in xml which has html table but it has xml markup( >
and <
) for > and < which need to be repalced first as readHTMLTable function cannot handle xml markup.
And now the answer to your real question:
sample.xml with encoded table:
<Text>
<table>
<tr><td>1</td><td>2</td></tr>
<tr><td>2</td><td>8</td></tr>
<tr><td>4</td><td>32</td></tr>
</table>
</Text>
Read it in:
> library(XML)
> doc = xmlTreeParse("sample.xml", useInternal = TRUE)
> top<-xmlRoot(doc)
Convert to text:
> table=xmlValue(top)
> table
[1] "\n<table>\n<tr><td>1</td><td>2</td></tr>\n<tr><td>2</td><td>8</td></tr>\n<tr><td>4</td><td>32</td></tr>\n</table>\n"
This is now ready to feed to readHTMLTable
. No string conversion needed:
> readHTMLTable(table)
$`NULL`
V1 V2
1 1 2
2 2 8
3 4 32
Howzat?
If your question is to know how to replace a string in the content of an XML node, then you can check the following code, using the sample.xml
file you provided :
## Parse the XML file
doc <- xmlTreeParse("sample.xml", useInternal = TRUE)
## Select the nodes we want to update
nodes <- getNodeSet(doc, "//Text")
## For each node, apply gsub on the content of the node
lapply(nodes, function(n) {
xmlValue(n) <- gsub("ABC","foobar",xmlValue(n))
})
Which will give you :
R> doc
<?xml version="1.0"?>
<Text>
<p>foobar </p>
</Text>
Here you can see that "ABC" as been replaced by "foobar".
But, if you try this code with the substitution you want to achieve (replace "<" wit "<"), it apparently won't work :
doc <- xmlTreeParse("sample.xml", useInternal = TRUE)
nodes <- getNodeSet(doc, "//Text")
lapply(nodes, function(n) {
xmlValue(n) <- gsub("<","<",xmlValue(n))
})
will give you :
R> doc
<?xml version="1.0"?>
<Text>
<p>ABC </p>
</Text>
Why ? If you are working with XML files, you should know that some characters, mainly <, >, & and " are reserved as they are part of the base XML syntax. As such, they cannot appear in the content of the nodes, otherwise parsing would fail. So they are replaced by entities, which are a sort of coding of these characters. For example, "<" is coded as "<", "&" is coded as "&", etc.
So here, the content of your node contains a "<" character, which has been automatically converted to his entity "<". What you try to do with your code is to replace "<" back with "<", which R will gladly do for you, but as it is a text content of a node, the XML package will immediatly convert it back to "<".
So, if what you want to achieve is to convert your string "<p>ABC </p>" to a new XML node "<p>ABC </p>", you can't do it that way. A solution would be to parse your text string, detect the name and of the node (here, "p") from it, create a new node with xmlNode()
, give it the text content "ABC" and replace the string with the node you just created.
Another quick and dirty way to do it would be first to replace all the entities in your file without parsing the XML. Something like this :
txt <- readLines(file("sample.xml"))
txt <- gsub("<", "<", txt)
txt <- gsub(">", ">", txt)
writeLines(txt, file("sample2.xml"))
doc2 <- xmlTreeParse("sample2.xml", useInternal = TRUE)
Which gives :
R> doc2
<?xml version="1.0"?>
<Text>
<p>ABC </p>
</Text>
But this is dangerous, because if there is a "real" "<" entity in you file, parsing will fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With