I have a XML parser using StAX
and I am using it to parse a huge file. However, I want to bring the time down as low as possible. I am reading the values putting it into an array and sending it off to another function to evaluate. I am calling the displayName
tag and it should go to the next xml as soon as it grabs the name instead of reading the whole xml file. I am looking for the fastest approach.
Java:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
public class Driver {
private static boolean bname;
public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
File file = new File("C:\\Users\\Robert\\Desktop\\root\\SDKCode\\src\\main\\java\\com\\example\\xmlClass\\data.xml");
parser(file);
}
public static void parser(File file) throws FileNotFoundException, XMLStreamException {
bname = false;
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(file));
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
// This will trigger when the tag is of type <...>
if (event.isStartElement()) {
StartElement element = (StartElement) event;
Iterator<Attribute> iterator = element.getAttributes();
while (iterator.hasNext()) {
Attribute attribute = iterator.next();
QName name = attribute.getName();
String value = attribute.getValue();
System.out.println(name + " = " + value);
}
if (element.getName().toString().equalsIgnoreCase("displayName")) {
bname = true;
}
}
if (event.isEndElement()) {
EndElement element = (EndElement) event;
if (element.getName().toString().equalsIgnoreCase("displayName")) {
bname = false;
}
}
if (event.isCharacters()) {
// Depending upon the tag opened the data is retrieved .
Characters element = (Characters) event;
if (bname) {
System.out.println(element.getData());
}
}
}
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<results
xmlns="urn:www-collation-com:1.0"
xmlns:coll="urn:www-collation-com:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:www-collation-com:1.0
urn:www-collation-com:1.0/results.xsd">
<WebServiceImpl array="1"
guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService">
<isPlaceholder>false</isPlaceholder>
<displayName>server.servername1.siqom.siqom.us.com</displayName>
<hierarchyType>WebService</hierarchyType>
<hierarchyDomain>app.web</hierarchyDomain>
</WebServiceImpl>
</results>
<?xml version="1.0" encoding="UTF-8"?>
<results
xmlns="urn:www-collation-com:1.0"
xmlns:coll="urn:www-collation-com:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:www-collation-com:1.0
urn:www-collation-com:1.0/results.xsd">
<WebServiceImpl array="1"
guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService">
<isPlaceholder>false</isPlaceholder>
<displayName>server.servername2.siqom.siqom.us.com</displayName>
<hierarchyType>WebService</hierarchyType>
<hierarchyDomain>app.web</hierarchyDomain>
</WebServiceImpl>
</results>
<?xml version="1.0" encoding="UTF-8"?>
<results
xmlns="urn:www-collation-com:1.0"
xmlns:coll="urn:www-collation-com:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:www-collation-com:1.0
urn:www-collation-com:1.0/results.xsd">
<WebServiceImpl array="1"
guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService">
<isPlaceholder>false</isPlaceholder>
<displayName>server.servername3.siqom.siqom.us.com</displayName>
<hierarchyType>WebService</hierarchyType>
<hierarchyDomain>app.web</hierarchyDomain>
</WebServiceImpl>
</results>
etc...