java - 仅最后一个值被添加到 JSON 对象

标签 java json xml-parsing

我有一个包含大量数据的 xml 文件。我已经解析了 xml 文件并尝试将值添加到 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>

我错过了什么吗?

最佳答案

创建一个 JSONArray 并继续在循环内插入 JSONObject

 JSONArray finalArray = new JSONArray(); // create your jsonarray
 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")) {
                 JSONObject json = new JSONObject(); // your temp obj

                 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));
                 finalArray.put(json); // push your values in the array
             }
         }

     }
 }
 //System.out.println("result-array:" +arrayvalue);
 System.out.println("result " + finalArray);

关于java - 仅最后一个值被添加到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45437349/

相关文章:

java - Libgdx、碰撞、2 个矩形、旋转、菜鸟

java - 无法从 try-catch 嵌套 for 循环中提取字符串

javascript - 如何解析具有类似 XML 结构的文件,但在内容旁边带有自闭合标签(而不是包含内容)

c++ - 使用 RapidXml 解析日文汉字时出错

java - 列为 O(log(n))

java - 考虑 AM/PM 计算 Java 中的日期/时间差

Java解析args中的JSON

Javascript - 解析html字符串获取具有类X的每个元素并返回它们的数组

android - 明智地从 JSON 类别中获取数据

java - 登录后Jsp和servlet与用户的getAttribute错误