R xpathSApply --> extracting Attribute gives empty result - r

I try to parse out the xmlValue for the attribute "NAME" in an XML Document in R.
<NN ID_NAME="107232" ID_NTYP="6" NAME="dSpace_ECat1Error.STS" KOMMENTAR="dSpace_ECat1Error.STS" IS_SYSTEM="0" IS_LOCKED="0" DTYP="Ganzzahl" ADIM="" AFMT=""/><NN ID_NAME="107233" ID_NTYP="6" NAME="dSpace_ECat2Error.STS" KOMMENTAR="dSpace_ECat2Error.STS" IS_SYSTEM="0" IS_LOCKED="0" DTYP="Ganzzahl" ADIM="" AFMT=""/>
The result should be like this:
dSpace_ECat1Error.STS
dSpace_ECat2Error.STS
I use this function:
xpathSApply(root,"//NN[#NAME]",xmlValue)
But as a result, I get just empty "" (Quotes)
What have I done wrong?
Thank's in advance!

I just found out by using:
erg<-xpathSApply(root,"//NN",xmlGetAttr,'NAME')
There should be a better tutorial for this particular XML-function in R....

Related

JSONPath to get parameter from an array

I have a response body I believe is an array it looks like this:
{"id":982,"form_stage":"1","customer_hash":"fffae165253f494a95bdb753ca87716c"}
In this case I want to get "customer_hash" and I have tried every combination of $..customer_hash $.customer_hash, etc but none of them work because this doesn't start with "response" or "result" I guess it is an array not an object(?).
Can anyone tell me what would be the JSONPath mapping to get the parameter "customer_hash"?
Try using:
$.customer_hash
Output:
[
"fffae165253f494a95bdb753ca87716c"
]

JSON format in R refuse to parse?

Here is my toy JSON:
"[[143828095,86.82525,78.50037,0.011764707,1.0,1,1],
[143828107,86.82525,78.50037,0.015686275,1.0,1,0],
[143828174,84.82802,83.49646,0.015686275,1.0,1,0],
[143828190,83.3301,92.4895,0.011764707,1.0,1,0],
[143828206,83.3301,92.4895,0.011764707,1.0,1,-1],
[143828251,119.482666,98.4848,0.03137255,1.0,2,1],
[143828325,123.30899,95.93237,0.027450982,1.0,2,0],
[143828334,128.47015,92.4895,0.027450982,1.0,2,0],
[143828351,128.47015,92.4895,0.027450982,1.0,2,-1],
[143828406,115.19141,60.514465,0.019607844,1.0,3,1],
[143828529,121.183105,61.51367,0.019607844,1.0,3,0],
[143828551,121.183105,61.51367,0.019607844,1.0,3,-1],
[143828696,105.502075,94.26935,0.023529414,1.0,8,1],
[143828773,105.502075,94.26935,0.023529414,1.0,8,-1],
[143829030,78.24274,58.18811,0.023529414,1.0,DEL,1],
[143829107,78.24274,58.18811,0.023529414,1.0,DEL,-1],
[143831178,127.47159,76.28339,0.023529414,1.0,8,1],
[143831244,127.47159,76.28339,0.023529414,1.0,8,-1]]"
Now I want to parse it (fromJSON()) but
DEL
within the JSON prevents me to do this.
Please advise how to fix it.
You can substitute "DEL" for, say, 0.
json_string <- "[[143828095,86.82525,78.50037,0.011764707,1.0,1,1], [143828107,86.82525,78.50037,0.015686275,1.0,1,0], [143828174,84.82802,83.49646,0.015686275,1.0,1,0], [143828190,83.3301,92.4895,0.011764707,1.0,1,0], [143828206,83.3301,92.4895,0.011764707,1.0,1,-1], [143828251,119.482666,98.4848,0.03137255,1.0,2,1], [143828325,123.30899,95.93237,0.027450982,1.0,2,0], [143828334,128.47015,92.4895,0.027450982,1.0,2,0], [143828351,128.47015,92.4895,0.027450982,1.0,2,-1], [143828406,115.19141,60.514465,0.019607844,1.0,3,1], [143828529,121.183105,61.51367,0.019607844,1.0,3,0], [143828551,121.183105,61.51367,0.019607844,1.0,3,-1], [143828696,105.502075,94.26935,0.023529414,1.0,8,1], [143828773,105.502075,94.26935,0.023529414,1.0,8,-1], [143829030,78.24274,58.18811,0.023529414,1.0,DEL,1], [143829107,78.24274,58.18811,0.023529414,1.0,DEL,-1], [143831178,127.47159,76.28339,0.023529414,1.0,8,1], [143831244,127.47159,76.28339,0.023529414,1.0,8,-1]]"
json_string <- gsub("DEL", 0, json_string) # You can make the zero any number you like
fromJSON(json_string)
using a json parser (http://json.parser.online.fr/), just deleting the "DEL" at the respective places seems to fix the issue.

NodeSet as character

I want to get a NodeSet, with the getNodeSet function from the XML package, and write it as text in a file.
For example :
> getNodeSet(htmlParse("http://www.google.fr/"), "//div[#id='hplogo']")[[1]]
<div title="Google" align="left" id="hplogo" onload="window.lol&&lol()" style="height:110px;width:276px;background:url(/images/srpr/logo9w.png) no-repeat">
<div nowrap="" style="color:#777;font-size:16px;font-weight:bold;position:relative;top:70px;left:218px">France</div>
</div>
I want to save all this node unchanged in a file.
The problem is we can't write the object directly with :
write.lines(getNodeSet(...), file)
And as.character(getNodeSet(...)) returns a C pointer.
How can I do this ? Thank you.
To save an XML object to a file, use saveXML, e.g.,
url = "http://www.google.fr/"
nodes = getNodeSet(htmlParse(url), "//div[#id='hplogo']")[[1]]
fl <- saveXML(nodes, tempfile())
readLines(fl)
There has to be a better way, until then you can capture what the print method for a XMLNode outputs:
nodes <- getNodeSet(...)
sapply(nodes, function(x)paste(capture.output(print(x)), collapse = ""))
I know it might be a bit outdated but i got into the same problem and wanted to leave it for future reference, after searching and struggling the answer is as simple as:
htmlnodes <- toString(nodes)
write.lines(htmlnodes, file)

How to use function with variable in R?

in R,the pdf function can save graph in c:/test:
pdf("c:/test")
I want to make a variable substitue pdf ,how can i make it run ?
str<-"pdf"
str("c:/test")
get() does this:
get(str)("c:/test")
s = "pdf" ; do.call(s, list("c:/test"))
or in two steps,
cl <- call(s, "c:/test")
eval(cl)
You can extract the function specified by the name in str with match.fun:
match.fun(str)("c:/test")
By the way: It is not a good idea to name an object str since this is the name of a basic function in R.

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Resources