October 20, 2015 Re: kxml - parsing AWS API xml respond | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | I think part of the issue here is that holo isn't quite sure of what information [s]he needs out of the XML reply and how to do that with XPath. The first post mentioned getting instanceId and launchTime, so I'll start there using the AWS example XML found here: https://github.com/polopoly/cloud-ui/blob/master/aws/examples/DescribeInstances.xml You can find all parent nodes that provide this set of information with XPath query "//instancesSet/item". From there, you can iterate over the list and pull out the information you need: auto parents = root.parseXPath("//instancesSet/item"); foreach(parent; parents) { string instanceId = parent.parseXPath("instanceId")[0].getCData(); string launchTime = parent.parseXPath("launchTime")[0].getCData(); } In the process of checking this out, I discovered that subnode predicates are broken (attribute predicates work just fine, I need to write more unittests). I guess I have some things to work on tonight. |
October 20, 2015 Re: kxml - parsing AWS API xml respond | ||||
---|---|---|---|---|
| ||||
Posted in reply to opticron | On Tuesday, 20 October 2015 at 13:23:47 UTC, opticron wrote: > I think part of the issue here is that holo isn't quite sure of what information [s]he needs out of the XML reply and how to do that with XPath. The first post mentioned getting instanceId and launchTime, so I'll start there using the AWS example XML found here: https://github.com/polopoly/cloud-ui/blob/master/aws/examples/DescribeInstances.xml > > You can find all parent nodes that provide this set of information with XPath query "//instancesSet/item". From there, you can iterate over the list and pull out the information you need: > > auto parents = root.parseXPath("//instancesSet/item"); > foreach(parent; parents) { > string instanceId = parent.parseXPath("instanceId")[0].getCData(); > string launchTime = parent.parseXPath("launchTime")[0].getCData(); > } > > In the process of checking this out, I discovered that subnode predicates are broken (attribute predicates work just fine, I need to write more unittests). I guess I have some things to work on tonight. Sorry i augment you work ;) . I was continuing with my project i came across next problem. When im checking instance name with such code: auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`)[0].goCData; it is compiling properly but it is breaking program when is no name set. I make quick workaround: auto tmp = list.parseXPath(`//tagSet/item[key="Name"]/value`); if(tmp == null) { instances[tmpinst].instanceName = ""; } else { auto instances[tmpinst].instanceName = tmp[0].getCData; } but is there any reason why it is not taken into consideration in "getCData" method? |
October 21, 2015 Re: kxml - parsing AWS API xml respond | ||||
---|---|---|---|---|
| ||||
Posted in reply to holo | On Tuesday, 20 October 2015 at 16:53:19 UTC, holo wrote: > When im checking instance name with such code: > > auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`)[0].goCData; > > it is compiling properly but it is breaking program when is no name set. > > I make quick workaround: > > auto tmp = list.parseXPath(`//tagSet/item[key="Name"]/value`); > if(tmp == null) > { > instances[tmpinst].instanceName = ""; > } > else > { > auto instances[tmpinst].instanceName = tmp[0].getCData; > } > > > but is there any reason why it is not taken into consideration in "getCData" method? The issue you're running into is that parseXPath always returns an array of results, even when there are zero or only 1 result. kxml can't know in advance how many results there will be for the given query, so it will always return an array no matter how many results are found. To work around this issue, you could define a function like so: string getCData(XmlNode[]nodes) { if (!nodes.length) return ""; return nodes[0].getCData(); } and in use: auto test = list.parseXPath(`//tagSet/item[key="Name"]/value`).getCData(); This takes advantage of UFCS to keep the call chaining and hide [0] while handling the possibility of an empty list. |
Copyright © 1999-2021 by the D Language Foundation