美好的一天,
我一直在尝试使用VTD解析器来提高当前DOM代码的性能,但是,我仍然看到DOM更快地解析,请告诉我哪里出错了。
这是我的代码示例以及示例XML
示例XML -
<account-detail-result><status><status-code>0</status-code><status-message></status-message></status><result>
<line><entrydate><entryyear>2016</entryyear><entrymonth>02</entrymonth><entryday>16</entryday></entrydate><date><year>2016</year><month>02</month><day>15</day></date><merchant></merchant><description>DESCRIPTION1184076</description><transcode>246</transcode><rollingbalance>96.76</rollingbalance><indicator>DR</indicator><indicator>DR</indicator><txnseqno>366951</txnseqno><entrytime>50221</entrytime><isolbpendingtransaction>false</isolbpendingtransaction><valuedate>2016-02-15</valuedate><amount type='DR'>1000.00</amount><cardnumber></cardnumber></line>
VTD代码 -
VTDGen vg = new VTDGen();
vg.setDoc(vResultString.getBytes());
vg.parse(false);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/account-detail-result/status");
while (ap.evalXPath() != -1) {
if (vn.toElement(VTDNav.FIRST_CHILD, "status-code")) {
if (vn.getText() != -1) {
vStatusCode = vn.toNormalizedString(vn.getText());
vn.toElement(VTDNav.PARENT);
}
}
if (vn.toElement(VTDNav.FIRST_CHILD, "status-description")) {
if (vn.getText() != -1) {
vStatusDescription = vn.toNormalizedString(vn.getText());
vn.toElement(VTDNav.PARENT);
}
}
}
ap.resetXPath();
ap.selectXPath("/account-detail-result/result/line");
while (ap.evalXPath() != -1) {
do {
int venYear = 0;
int venMonth = 0;
int venDay = 0;
if (vn.toElement(VTDNav.FIRST_CHILD, "entrydate")) {
if (vn.toElement(VTDNav.FIRST_CHILD, "entryyear")) {
if (vn.getText() != -1) {
venYear = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
if (vn.toElement(VTDNav.FIRST_CHILD, "entrymonth")) {
if (vn.getText() != -1) {
venMonth = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
if (vn.toElement(VTDNav.FIRST_CHILD, "entryday")) {
if (vn.getText() != -1) {
venDay = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
vn.toElement(VTDNav.PARENT);
vPostedDateCol = new DateTime(venYear, venMonth, venDay, 0, 0, 0).toDate();
}
if (vn.toElement(VTDNav.FIRST_CHILD, "date")) {
if (vn.toElement(VTDNav.FIRST_CHILD, "year")) {
if (vn.getText() != -1) {
vYear = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
if (vn.toElement(VTDNav.FIRST_CHILD, "month")) {
if (vn.getText() != -1) {
vMonth = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
if (vn.toElement(VTDNav.FIRST_CHILD, "day")) {
if (vn.getText() != -1) {
vDay = Integer.valueOf(vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
}
vn.toElement(VTDNav.PARENT);
}
if (vn.toElement(VTDNav.FIRST_CHILD, "description")) {
if (vn.getText() != -1) {
vDescription = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "transcode")) {
if (vn.getText() != -1) {
vTransactionCode = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "rollingbalance")) {
if (vn.getText() != -1) {
vRollingBalance = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "indicator")) {
if (vn.getText() != -1) {
System.out.println(vn.toNormalizedString(vn.getText()));
vIndicator = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "txnseqno")) {
if (vn.getText() != -1) {
vTxnSeqNo = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "entrytime")) {
if (vn.getText() != -1) {
vEntryTime = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "isolbpendingtransaction")) {
if (vn.getText() != -1) {
isOLBPendingTransaction = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "valuedate")) {
if (vn.getText() != -1) {
valueDate = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "status")) {
if (vn.getText() != -1) {
vStatus = vn.toNormalizedString(vn.getText());
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "amount")) {
if (vn.getText() != -1 && vn.getAttrVal("type") != -1) {
vAmount = vn.toNormalizedString(vn.getText());
vAmountType = vn.toNormalizedString(vn.getAttrVal("type"));
}
}
if (vn.toElement(VTDNav.NEXT_SIBLING, "cardnumber")) {
if (vn.getText() != -1) {
cardNumber = vn.toNormalizedString(vn.getText());
}
}
vn.toElement(VTDNav.PARENT);
} while (vn.toElement(VTDNav.NEXT_SIBLING, "line"));
}
ap.resetXPath();
答案 0 :(得分:0)
首先,我对您的代码有一些评论:
此行将内存中的字符串转换为字节数组。为什么不直接从使用parseFile()的文件中读取它?
vg.setDoc(vResultString.getBytes());
此声明
if(vn.getText()!= -1){
vStatusCode = vn.toNormalizedString(vn.getText());
vn.toElement(VTDNav.PARENT);
}
应该是
if(vn.getText()!= -1){
vStatusCode = vn.toNormalizedString(vn.getText());
}
vn.toElement(VTDNav.PARENT);
请注意,如果getText()返回-1,则游标不会返回其父节点,从而导致xpath逻辑错误。
以下陈述
venYear = Integer.valueOf(vn.toNormalizedString(vn.getText()));
可以改写为
venYear = vn.parseInt(vn.getText()));
除此之外,我无法为您提供有关如何调整代码的更多建议,而无需使用代码和xml数据完成完整的测试用例。
您是否运行了jdk的分析器来识别瓶颈?