StAX
的XML解析器,正在使用它来解析一个巨大的文件。然而,我想尽可能地缩短时间。我正在读取值并将其放入数组中,然后将其发送到另一个函数进行评估。我调用displayName
标记,并且它应该在获取名称后立即转到下一个xml,而不是读取整个xml文件。我正在寻找最快的方法。
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...