org.json.JSONException:在JAVA中复制键“x”

时间:2013-12-03 11:26:07

标签: java xml json

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions>
  <bpmn2:process id="defaultPackage.NewProcess2">
    <bpmn2:startEvent id="StartEvent_1" name="StartProcess">
      <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:userTask id="UserTask_1" name="User Task 1">
      <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:userTask id="UserTask_2" name="User Task 2">
      <bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_4</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:endEvent id="EndEvent_1" name="end event">
      <bpmn2:incoming>SequenceFlow_4</bpmn2:incoming>
    </bpmn2:endEvent>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_Process_1" bpmnElement="defaultPackage.NewProcess2">
      <bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="StartEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="60.0" y="210.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">
        <dc:Bounds height="50.0" width="110.0" x="165.0" y="205.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_UserTask_2" bpmnElement="UserTask_2">
        <dc:Bounds height="50.0" width="110.0" x="550.0" y="203.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_EndEvent_1" bpmnElement="EndEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="842.0" y="202.0"/>
</bpmn2:definitions>

对于在<bpmn2:process> </bpmn2:process>标签下定义的每个userTask节点,我想获得由x,y坐标定义的theire位置,该位置位于:

<bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">
        <dc:Bounds height="50.0" width="110.0" x="165.0" y="205.0"/>
      </bpmndi:BPMNShape>

<bpmndi:BPMNDiagram></bpmndi:BPMNDiagram>个标签内。

我想将x,y坐标附加到我构建的JSON字符串,以生成每个userTask节点的属性。

我尝试了以下代码:

public static void generateChildNodesDefinitions(Node node) throws JSONException
    {
        if (node != null && node.hasChildNodes())
        {

            jw.key("nodes").array();

            NodeList childnodelist = node.getChildNodes();

            for (int k = 0; k < childnodelist.getLength(); k++)
            {
                Node childn = childnodelist.item(k);

                if (childn.hasAttributes() && childn.getNodeName() != "bpmn2:sequenceFlow")
                {

                    ArrayList<String> list = (ArrayList<String>) jsonValues.get(childn.getNodeName());

                    NamedNodeMap nnmchildnodes = childn.getAttributes();
                    //per cdo nyje duhet te krijoj nje json object
                    jw.object();


                    if (nnmchildnodes != null)
                    {
                        for (int j = 0; j < nnmchildnodes.getLength(); j++)
                        {


                            Node nodeUid = nnmchildnodes.item(j);

                            if (nodeUid.getNodeName().equals("id"))
                            {
                                String uid = new UID().toString();

                                hmIDs.put(nodeUid.getNodeValue(), uid);

                                nodeUid.setNodeValue(uid);

                            }



                            jw.key(nodeUid.getNodeName()).value(nodeUid.getNodeValue());
                        }
                    }


                    if (list != null && !list.isEmpty())
                    {
    List<String> myXarray = new ArrayList<String>();
                        List<String> myYarray = new ArrayList<String>(); 

                        ArrayList<String> bpmnElements = getElementAttributes("bpmndi:BPMNShape", "bpmnElement");
                        for (int j = 0; j < bpmnElements.size(); j++)
                        {
                            String bpmnElement = bpmnElements.get(j);
                          if(childn.getNodeName().equals("bpmn2:userTask")){
                              ArrayList<String> idUserTask = getIds("bpmn2:userTask");


                              for(int l=0; l < idUserTask.size(); l++){
                                  if(idUserTask.get(l).equals(bpmnElement)){

                                      ArrayList<String> Xs = getElementAttributes ("dc:Bounds" , "x");
                                      ArrayList<String> Ys = getElementAttributes ("dc:Bounds" , "y");

                                      String x = Xs.get(j);
                                      String y = Ys.get(j); 
                                      myXarray.add(x);
                                      myYarray.add(y);

                                     }
                                  }
                          }
                        }

                                      jw.key("x").value(myXarray);
                                      jw.key("y").value(myYarray);

}

1 个答案:

答案 0 :(得分:0)

错误是因为您为每次迭代设置了密钥jw.key("x").value(x);jw.key("y").value(y);,它就像重复一样。

最好将x的值设置为数组,然后jw.key("x").value(yourXarray);jw.key("y").value(yourYarray);

List<String> yourXarray = new ArrayList<String>();
 for(int l=0; l<idUserTask.size(); l++){ 
    if(idUserTask.get(l).equals(bpmnElement)){
      ArrayList<String> Xs = getElementAttributes ("dc:Bounds" , "x");
       String x = Xs.get(j);
       yourXarray.add(x)
     }}
 jw.key("x").value(yourXarray);