我有一個包含大量數據的xml文件。我已解析的XML文件,並試圖將值添加到JSON對象,但只有最後一個值越來越added.Please找到我的代碼如下:只有最後一個值被添加到JSON對象
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Process_Parser {
static JSONObject json= new JSONObject();
//static //JSONObject arrayvalue=new JSONArray();
public static void main(String args[]) {
try {
File process = new File("/Users/instrument.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(process);
doc.getDocumentElement().normalize();
//System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("process");
//System.out.println("==========================");
Element pageElement = (Element)doc.getElementsByTagName("process").item(0);
//NodeList result = pageElement.getElementsByTagName("processName");
System.out.println("Suba-----------"+nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
//System.out.println(getValue("processName", element).contains("carkitd"));
//if(element.getElementsByTagName("process") != null){
if(element.getFirstChild().getNodeValue() != null){
if(getValue("processName", element).contains("carkitd")){
json.put("processName",getValue("processName", element));
json.put("cpuUsage",getValue("cpuUsage", element));
json.put("realmemory",getValue("realmemory", element));
json.put("Virtualmemory",getValue("Virtualmemory", element));
json.put("thread",getValue("thread", element));
json.put("cputime",getValue("cputime", element));
}
}
}
}
//System.out.println("result-array:" +arrayvalue);
System.out.println("result" +json);
//}
//System.out.println("arrayvalue::"+arrayvalue.size());
}catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}
當我運行上面的代碼,我得到下面的結果
實際結果:
{
"Virtualmemory":"1100320.000000",
"cpuUsage":"0.000000",
"process":"carkitd",
"realmemory":"1044.000000",
"thread":"2.000000",
"cputime":"0.000000"
}
預期結果:
它應該有67個物體
示例XML文件:
<instrument>
<slice time ='1498215480919'>
<process>
<processId>1.000000</processId>
<processName>launchd</processName>
<cpuUsage>0.203195</cpuUsage>
<realmemory>5084.000000</realmemory>
<Virtualmemory>1080112.000000</Virtualmemory>
<thread>7.000000</thread>
<cputime>0.000000</cputime>
</process>
<process>
<processId>24.000000</processId>
<processName>carkitd</processName>
<cpuUsage>0.002845</cpuUsage>
<realmemory>576.000000</realmemory>
<Virtualmemory>1074752.000000</Virtualmemory>
<thread>6.000000</thread>
<cputime>0.000000</cputime>
</process>
<process>
<processId>24.000000</processId>
<processName>carkitd</processName>
<cpuUsage>0.002845</cpuUsage>
<realmemory>576.000000</realmemory>
<Virtualmemory>1074752.000000</Virtualmemory>
<thread>6.000000</thread>
<cputime>0.000000</cputime>
</process>
</slice>
</instrument>
我失去了一些東西?
通過在for循環中創建對象並在對象填充後將對象添加到數組中,使用'JSONObject'的'JSONArray' –