| Thread overview |
|---|
October 19, 2015 kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
I'm trying to take out from AWS respond needed information. Here is cut off part of example respond:
...
<instanceId>i-xxxxx</instanceId>
<imageId>ami-xxxxx</imageId>
<instanceState>
<code>16</code>
<name>running</name>
</instanceState>
<privateDnsName>ip-xxxxxxx.ec2.internal</privateDnsName>
<dnsName>ec2-xxxxxx.compute-1.amazonaws.com</dnsName>
<reason/>
<keyName>ec2key</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
<productCodes>
<item>
<productCode>xxxxxxxxxxxxxxxxxxxx</productCode>
<type>marketplace</type>
</item>
</productCodes>
<instanceType>c3.large</instanceType>
<launchTime>2015-08-18T16:51:11.000Z</launchTime>
...
With such code im getting only information about all instance ids:
string xmlstring = cast(string)read("test.xml");
XmlNode newdoc = xmlstring.readDocument();
XmlNode[] searchlist = newdoc.parseXPath("//instanceId");
writeln(searchlist);
What i need is that to take out from there instance id and eg LaunchTime for that instance. How can i do it using kxml?
Why there is needed that "//" before tag? In other way im not getting any respond. What is interesting when i do that same with first tag appearing in respond it is not needed. From other hand with "///" i'm getting much more information but not separated by coma.
How to drop tags from respond? I have such result of parsing by id tag:
<instanceId>i-xxxxxx</instanceId>
I need only value. Is there some equivalent to ".text" from std.xml?
| ||||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to holo | On Monday, 19 October 2015 at 04:49:25 UTC, holo wrote: > Why there is needed that "//" before tag? That's an XPath expression. > How to drop tags from respond? I have such result of parsing by id tag: > > <instanceId>i-xxxxxx</instanceId> > > I need only value. Is there some equivalent to ".text" from std.xml? Try getCData https://github.com/opticron/kxml/blob/master/source/kxml/xml.d#L309 | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Monday, 19 October 2015 at 11:53:08 UTC, Kagamin wrote: > On Monday, 19 October 2015 at 04:49:25 UTC, holo wrote: >> Why there is needed that "//" before tag? > > That's an XPath expression. > Need, to read about that XPaths. >> How to drop tags from respond? I have such result of parsing by id tag: >> >> <instanceId>i-xxxxxx</instanceId> >> >> I need only value. Is there some equivalent to ".text" from std.xml? > > Try getCData https://github.com/opticron/kxml/blob/master/source/kxml/xml.d#L309 getCData is working like expected. Thank you for tips. | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | Sorry i was trying to use that XPaths to take out instanceId and laounchTime but i always get an empty respond. //instanceId/launchTime" - empty //instanceId/* - empty too //instanceId.launchTime - empty too How to take instance id and variable which im interest in or few/all for instanceId? | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to holo | Select parent tags and get data from their child tags like instanceId. | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Monday, 19 October 2015 at 12:54:52 UTC, Kagamin wrote: > Select parent tags and get data from their child tags like instanceId. void main() { string xmlstring = cast(string)read("test.xml"); XmlNode newdoc = xmlstring.readDocument(); XmlNode[] searchlist = newdoc.parseXPath("//instanceId"); XmlNode[] searchlist2 = searchlist.parseXPath("//launchTime"); writeln(searchlist2); } Not working but probably its not what you thought, how to select it do you have some example? | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to holo | If you don't want parent tag, then: XmlNode[] searchlist2 = newdoc.parseXPath("//launchTime"); | |||
October 19, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Monday, 19 October 2015 at 16:12:56 UTC, Kagamin wrote: > If you don't want parent tag, then: > XmlNode[] searchlist2 = newdoc.parseXPath("//launchTime"); I wanted parent tag, i think. I almost figure out what i needed, but getCData stops working :/ 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); } } [holo@ultraxps test]$ 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(20,57): Error: no property 'getCData' for type 'XmlNode[]' dmd failed with exit code 1. [holo@ultraxps test]$ | |||
October 20, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to holo | I have no idea what i'm doing, but i did it (i just choose 1 element of array):
void main()
{
string xmlstring = cast(string)read("test.xml");
XmlNode newdoc = xmlstring.readDocument();
XmlNode[] searchlist = newdoc.parseXPath(`//instancesSet/item`);
foreach(list; searchlist)
{
string test1 = list.parseXPath(`//instanceId`)[0].getCData;
writeln(test1);
}
}
Is it really how it should looks like? Is there any better/more beautiful solution?
| |||
October 20, 2015 Re: kxml - parsing AWS API xml respond | ||||
|---|---|---|---|---|
| ||||
Posted in reply to holo | You can write a helper:
XmlNode selectSingleNode(XmlNode src, string path)
{
XmlNode[] nodes = src.parseXPath(path);
return nodes.length==0 ? null : nodes[0];
}
Then:
string test1 = node.selectSingleNode(`//instanceId`).getCData();
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply