문제

I'm using JDOM for the first time and so far, not very successfully. Also, I'm a .NET guy who was assigned a Java project.

I have this xml file and I'm trying to read all the elements.

This is the code I have so far. The response is where I get the xml data. I can read the root element, but I'm struggling to read the elements. Any ideas??

 JSONObject ticket  = data.getDataJSON();
    String id       = ticket.getString(SDPTicketKeyValues.Key.ID);
    String input_data   = "";

    Content response = Request
            .Post(REST_URL + REQUEST_RESOURCE + id + "/" + NOTES_RESOURCE)
            .bodyForm(
                Form.form().add(TECHKEY_PARAMETER, MYTECH_KEY)
                       .add(OPERATION_PARAMETER, GETNOTES_OPERATION)
                       .add("INPUT_DATA", input_data)
                       .build())
        .execute().returnContent(); 

    SAXBuilder builder  = new SAXBuilder(); 
    StringReader reader     = new StringReader(response.asString());
    Document document   = (Document) builder.build(reader);
    Element rootNode    = document.getRootElement();

This is the xml file

<?xml version="1.0" encoding="UTF-8"?>
<API version="1.0">
    <response>
        <operation name="GET_NOTES">
            <result>
                <statuscode>200</statuscode>
                <status>Success</status>
                <message>Notes details fetched successfully.</message>
            </result>

            <Details>
                <Notes>
                    <Note URI="http://localhost:8080/sdpapi/request/10/notes/901/">
                        <parameter>
                        <name>isPublic</name>
                        <value>false</value>
                        </parameter>
                        <parameter>
                        <name>notesText</name>
                        <value>dfgfdgdfgdfgdgdgdgdgdg</value>
                        </parameter>
                        <parameter>
                        <name>userName</name>
                        <value>Howard Stern</value>
                        </parameter>
                        <parameter>
                        <name>notesDate</name>
                        <value>1373971580200</value>
                        </parameter>
                    </Note>

                    <Note URI="http://localhost:8080/sdpapi/request/10/notes/612/">
                        <parameter>
                        <name>isPublic</name>
                        <value>false</value>
                        </parameter>
                        <parameter>
                        <name>notesText</name>
                        <value>dfgdfgdfgdfgdfgdfgdfgdf</value>
                        </parameter>
                        <parameter>
                        <name>userName</name>
                        <value>Howard Stern</value>
                        </parameter>
                        <parameter>
                        <name>notesDate</name>
                        <value>1373967102396</value>
                        </parameter>
                    </Note>

                    <Note URI="http://localhost:8080/sdpapi/request/10/notes/611/">
                        <parameter>
                        <name>isPublic</name>
                        <value>true</value>
                        </parameter>
                        <parameter>
                        <name>notesText</name>
                        <value>dfgdfgdfgdfgdfgdfgdgdgdgd</value>
                        </parameter>
                        <parameter>
                        <name>userName</name>
                        <value>Howard Stern</value>
                        </parameter>
                        <parameter>
                        <name>notesDate</name>
                        <value>1373967097117</value>
                        </parameter>
                    </Note>
                </Notes>
            </Details>
        </operation>
    </response>
</API>
도움이 되었습니까?

해결책

I decided to do it in a different way and now it's working. Here'es the code.

Document xml        = loadXMLFromString(response.asString());
    NodeList notesList  =  xml.getElementsByTagName(XML_NOTE);
    List<Note> notes    = new ArrayList<Note>();

    for (int notesLoop = 0; notesLoop < notesList.getLength(); notesLoop++) {
        Element notesNode   = (Element) notesList.item(notesLoop);
        Note newNote        = new Note();

        String iD = notesNode.getAttribute("URI").substring(notesNode.getAttribute("URI").lastIndexOf("/"));
        newNote.setUri(notesNode.getAttribute("URI"));

        NodeList paramList = notesNode.getElementsByTagName(XML_PARAMETER);

        for (int paramsLoop = 0; paramsLoop < paramList.getLength(); paramsLoop++) {
            Element paramsNode = (Element) paramList.item(paramsLoop);
            Params ll = new Params();

            NodeList name   = paramsNode.getElementsByTagName("name");
            NodeList value  = paramsNode.getElementsByTagName("value");

            ll.setName(name.item(0).getFirstChild().getNodeValue());
            ll.setValue(value.item(0).getFirstChild().getNodeValue());

            newNote.getParams().add(ll);
        }

        notes.add(newNote);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top