I'm using in my project such library for XML parsing:
https://github.com/opticron/kxml/blob/master/source/kxml/xml.d
Here is my example code:
void main()
{
string xmlstring = cast(string)read("test.xml");
XmlNode newdoc = xmlstring.readDocument();
XmlNode[] searchlist = newdoc.parseXPath("//instancesSet/item");
foreach(list, searchlist)
{
string test = list.parseXPath("//instanceId").getCData();
writeln(test);
}
}
And it don't want to compile:
$ dub
Performing "debug" build using dmd for x86_64.
kxml 1.0.0: target for configuration "library" is up to date.
test ~master: building configuration "application"...
source/app.d(23,56): Error: no property 'getCData' for type 'XmlNode[]'
dmd failed with exit code 1.
When I'm using foreach
loop I thought it is taking only one value from whole array. So if that is array of XmlNode[]
variable, only one from it should be just XmlNode
(not array).
Why it still see there array? How to use getCData()
method to clear variable from tags and put it to string
?
In the loop,
list
is in fact anXmlNode
, but itsparseXPath()
returnsXmlNode[]
. If you want to work only with first result of XPath query in eachlist
, just add[0]
.