Separate your concerns - BACENGQueryXMLbyVTD
is both loading the data and executing the query.
First load the file into a byte[]
outside your loop, then pass it to BACENGQueryXMLbyVTD
. You might also want to pass the query
as an argument to the query
method.
You'll end up with a BACENGQueryXMLbyVTD
that looks like this (with the disclaimer that I'm not familiar with VTD-XML, so this might the creation of objects from that API might not work exactly like this):
public class BACENGQueryXMLbyVTD
{
private byte[] doc;
public BACENGQueryXMLbyVTD(byte[] doc)
{
this.doc = doc;
}
public List<String> query(String query) throws IOException, VTDException
{
VTDGen generator = new VTDGen();
generator.setDoc(doc);
generator.parse(false);
VTDNav navigator = generator.getNav();
AutoPilot autoPilot = new AutoPilot(navigator);
autoPilot.selectXPath(query);
List<String> nodeInterfaces = new ArrayList<String>();
int i;
while ((i = autoPilot.evalXPath()) != -1)
{
navigator.push();
nodeInterfaces.add(navigator.toString(i + 1));
}
return nodeInterfaces;
}
}
That you can then call like:
byte[] xmlContent = ... //load the xml from anywhere you like, not just a file as previously.
BACENGQueryXMLbyVTD getNodesInterfaces = new BACENGQueryXMLbyVTD(xmlContent);
for (String nodeName : nodeNames)
{
String query = "/import_data/group/node[@name='" + nodeName + "']/interface/@network_value";
nodeInterfaces = getNodesInterfaces.query(query);
...
}
You might also want to switch to the standard Java XPATH APIs - they're a lot clearer and better documented than VTD-XML.