ОЧЕНЬ сложно XML -> JSON парсинг с 3 массивами, чтобы затем вернуться к первому l oop через другой - PullRequest
0 голосов
/ 18 марта 2020

Это оригинал XML, который я конвертировал в JSON ниже. Я преобразовал его в JSON, потому что я думал, что будет легче разобрать, чем XML ... Используя приведенный ниже способ, я его преобразовал.

JSONObject jsonObjectXML = JSONML.toJSONObject(theXMLReturnedResult);

.

<?xml version="1.0" encoding="UTF-8"?><jp06:members xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/" xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/" jp:total-members="68">
    <jp06:member>
        <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob</jp06:url>
        <jp06:user-url>https://[ip address]:9443/jts/users/Bob</jp06:user-url>
        <jp06:role-assignments-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments</jp06:role-assignments-url>
        <jp06:role-assignments>
            <jp06:role-assignment>
                <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/tester</jp06:url>
                <jp06:role-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/tester</jp06:role-url>
            </jp06:role-assignment>
            <jp06:role-assignment>
                <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/default</jp06:url>
                <jp06:role-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/default</jp06:role-url>
            </jp06:role-assignment>
        </jp06:role-assignments>
        <jp06:role-assignments-editor-url>https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob&amp;rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles</jp06:role-assignments-editor-url>
    </jp06:member>
    <jp06:member>
        <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Ashley</jp06:url>
        <jp06:user-url>https://[ip address]:9443/jts/users/Ashley</jp06:user-url>
        <jp06:role-assignments-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Ashley/role-assignments</jp06:role-assignments-url>
        <jp06:role-assignments>
            <jp06:role-assignment>
                <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Ashley/role-assignments/tester</jp06:url>
                <jp06:role-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/tester</jp06:role-url>
            </jp06:role-assignment>
            <jp06:role-assignment>
                <jp06:url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Ashley/role-assignments/default</jp06:url>
                <jp06:role-url>https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/default</jp06:role-url>
            </jp06:role-assignment>
        </jp06:role-assignments>
        <jp06:role-assignments-editor-url>https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Ashley&amp;rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles</jp06:role-assignments-editor-url>
    </jp06:member>
    <jp06:member>
     ETC.......

Вот строка json:

{
"xmlns:jp": "http://jazz.net/xmlns/prod/jazz/process/1.0/",
"childNodes": [{
        "childNodes": [{
                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Bob"],
                "tagName": "jp06:url"
            }, {
                "childNodes": ["https://[ip address]:9443/jts/users/Bob"],
                "tagName": "jp06:user-url"
            }, {
                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Bob/role-assignments"],
                "tagName": "jp06:role-assignments-url"
            }, {
                "childNodes": [{
                        "childNodes": [{
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Bob/role-assignments/tester"],
                                "tagName": "jp06:url"
                            }, {
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles/tester"],
                                "tagName": "jp06:role-url"
                            }
                        ],
                        "tagName": "jp06:role-assignment"
                    }, {
                        "childNodes": [{
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Bob/role-assignments/default"],
                                "tagName": "jp06:url"
                            }, {
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles/default"],
                                "tagName": "jp06:role-url"
                            }
                        ],
                        "tagName": "jp06:role-assignment"
                    }
                ],
                "tagName": "jp06:role-assignments"
            }, {
                "childNodes": ["https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Bob&rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles"],
                "tagName": "jp06:role-assignments-editor-url"
            }
        ],
        "tagName": "jp06:member"
    }, {
        "childNodes": [{
                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Ashley"],
                "tagName": "jp06:url"
            }, {
                "childNodes": ["https://[ip address]:9443/jts/users/Ashley"],
                "tagName": "jp06:user-url"
            }, {
                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Ashley/role-assignments"],
                "tagName": "jp06:role-assignments-url"
            }, {
                "childNodes": [{
                        "childNodes": [{
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Ashley/role-assignments/tester"],
                                "tagName": "jp06:url"
                            }, {
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles/tester"],
                                "tagName": "jp06:role-url"
                            }
                        ],
                        "tagName": "jp06:role-assignment"
                    }, {
                        "childNodes": [{
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Ashley/role-assignments/default"],
                                "tagName": "jp06:url"
                            }, {
                                "childNodes": ["https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles/default"],
                                "tagName": "jp06:role-url"
                            }
                        ],
                        "tagName": "jp06:role-assignment"
                    }
                ],
                "tagName": "jp06:role-assignments"
            }, {
                "childNodes": ["https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/members/Ashley&rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoOInJ4V6/roles"],
                "tagName": "jp06:role-assignments-editor-url"
            }
        ],
        "tagName": "jp06:member"
    }, {
    ETC......

Итак, это код, который я пробовал:

JSONObject jsonObjectXML = JSONML.toJSONObject(theReturnedResult);
JSONArray jsonObject2 = jsonObjectXML.getJSONArray("childNodes");
JSONObject jsonObject       = jsonObject2.getJSONObject(0);
HashMap<String, Object> yourHashMap = new Gson().fromJson(jsonObject.toString(), HashMap.class);

Iterator hmIterator = yourHashMap.entrySet().iterator(); 

while (hmIterator.hasNext()) { 
     Map.Entry mapElement = (Map.Entry)hmIterator.next();
     System.out.print(yourHashMap);
     System.out.println(mapElement.getKey() + " " + mapElement.getValue()); 
} 

Значение yourHashMap :

{childNodes=[{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob], 
tagName=jp06:url}, {childNodes=[https://rwb40004.str4.ceti.cjis:9443/jts/users/Bob], 
tagName=jp06:user-url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments], 
tagName=jp06:role-assignments-url}, 
{childNodes=[{childNodes=[{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/tester], 
tagName=jp06:url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/tester], 
tagName=jp06:role-url}], 
tagName=jp06:role-assignment}, 
{childNodes=[{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/default], 
tagName=jp06:url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/default], 
tagName=jp06:role-url}], 
tagName=jp06:role-assignment}], 
tagName=jp06:role-assignments}, 
{childNodes=[https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob&rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles], 
tagName=jp06:role-assignments-editor-url}], 
tagName=jp06:member}childNodes [{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob], 
tagName=jp06:url}, 
{childNodes=[https://rwb40004.str4.ceti.cjis:9443/jts/users/Bob], 
tagName=jp06:user-url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments], 
tagName=jp06:role-assignments-url}, {childNodes=[{childNodes=[{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/tester], 
tagName=jp06:url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/tester], 
tagName=jp06:role-url}], 
tagName=jp06:role-assignment}, 
{childNodes=[{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob/role-assignments/default], 
tagName=jp06:url}, 
{childNodes=[https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles/default], 
tagName=jp06:role-url}], 
tagName=jp06:role-assignment}], 
tagName=jp06:role-assignments}, 
{childNodes=[https://[ip address]:9443/qm/_ajax-modules/com.ibm.team.process.manageProcessRolesAssignmentWidget?memberUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/members/Bob&rolesUrl=https://[ip address]:9443/qm/process/project-areas/eeoO8nJ4Vg/roles], 
tagName=jp06:role-assignments-editor-url}]

И оттуда он просто повторяет те же строки, что и раньше.

Это безумие - я не уверен, как go заботиться о каждом single childNodes , так что он продолжает цикл, чтобы получить других людей , но также и в этом single childNodes получить необходимую информацию, где tagName = jp06 : ролевые задания-URL . Затем сделайте резервную копию и получите URL этого childNodes .

Так что мне сложно обернуть голову, так как я работаю над это на несколько часов.

Если кто-то может помочь - пожалуйста, сделайте!

1 Ответ

0 голосов
/ 18 марта 2020

Хорошо, я в значительной степени решил это наконец !

JSONObject jsonObject = JSONML.toJSONObject(theXMLReturnedResult);
JSONArray jsonArray = jsonObject.getJSONArray("childNodes");

jsonArray.forEach(o -> {
    if (o instanceof JSONObject) {
        JSONObject crt              = (JSONObject) o;
        JSONArray jsonArray1        = crt.getJSONArray("childNodes");
        JSONObject jsonObject2      = jsonArray1.getJSONObject(0);

        if (jsonObject2.has("childNodes")) {
            String url      = jsonObject2.get("childNodes").toString()
                                         .replace("[\"", "").replace("\"]", "")
                                         .trim();
            String[] name   = url.replace("[\"", "")
                                 .replace("\"]", "")
                                 .trim()
                                 .split("/");                   
            mapHash.put(name[8], url);
        }
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...