美好的一天,VTD解析器性能VS DOM解析器
我一直在試圖用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();
所以它代表你的代碼作品,你只是希望它更有效率? – WhatsThePoint
@WhatsThePoint,是的代碼工作,但我想提高性能從樣本XML上發生的當前DOM解析,但是,與我目前的代碼,VTD似乎慢得多,請告知 – hsrjee
你的xml有多大?你如何衡量表現? –