3
我實際上做的是一個遞歸函數,它讀取xml中的標記。以下是代碼:在java中讀取XML標記,代碼優化
private void readTag(org.w3c.dom.Node item, String histoTags, String fileName, Hashtable<String, String> tagsInfos) {
try {
if (item.getNodeType() == Node.ELEMENT_NODE) {
NodeList itemChilds = item.getChildNodes();
for (int i=0; i < itemChilds.getLength(); i++) {
org.w3c.dom.Node itemChild = itemChilds.item(i);
readTag(itemChild, histoTags + "|" + item.getNodeName(), fileName, tagsInfos);
}
}
else if (item.getNodeType() == Node.TEXT_NODE) {
tagsInfosSoft.put(histoTags, item.getNodeValue());
}
}
該函數需要一些時間才能執行。函數讀取的xml格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Mouvement>
<Com>
<IdCom>32R01000000772669473</IdCom>
<RefCde>32R</RefCde>
<Edit>0</Edit>
<Com>
<Mouvement>
<Document>
有什麼辦法可以在java中優化這段代碼嗎?
您可以使用一些xml對象映射(例如使用xstream)來完成這項工作,**可能**效率更高。也許你可以發佈一個重現「慢」的麥克風? – 2016-04-29 04:48:41
@RC:你能舉個例子嗎? – tabby
請參閱http://x-stream.github.io/tutorial.html – 2016-04-29 07:22:05