It's all about the answers!

Ask a question

Get Attributes from XML-File (ResponseBody) in Java


Bastian W (217) | asked Jan 11 '16, 8:04 a.m.
edited Jan 12 '16, 5:35 a.m.
Hi together,

i have the following XML-File, implemented in Java as a String.
I got the file/xml from the Rest-Client via ResponseBody with

"https://localhost:9443/qm/process/project-areas"

<?xml version="1.0" encoding="UTF-8"?>

-<jp06:project-areas xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/">


-<jp06:project-area jp06:name="QM1">

<jp06:summary>REST CALLS</jp06:summary>

<jp06:description>This is a test</jp06:description>

<jp06:url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw</jp06:url>

<jp06:roles-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/roles</jp06:roles-url>

<jp06:links-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/links</jp06:links-url>

<jp06:members-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/members</jp06:members-url>

<jp:admins-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/admins</jp:admins-url>

<jp06:team-areas-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/team-areas</jp06:team-areas-url>

<jp06:project-admins-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/project-admins</jp06:project-admins-url>

<jp06:read-access-list-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/read-access-list</jp06:read-access-list-url>

<jp:timelines-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/timelines</jp:timelines-url>

<jp06:access-public>false</jp06:access-public>

<jp06:access-visible-to-access-list>false</jp06:access-visible-to-access-list>

<jp06:access-visible-to-members>true</jp06:access-visible-to-members>

<jp06:modified>Mon, 11 Jan 2016 11:30:11 GMT</jp06:modified>

<jp06:archived>false</jp06:archived>

<jp:history-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/history</jp:history-url>

</jp06:project-area>


+<jp06:project-area jp06:name="LifeCycleProject1 (Quality Management)">

</jp06:project-areas>

Now i have parsed the string into a XML-Object.

String responseBody (XML-Text);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                        DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader(responseBody)));

That works fine! Now i want to have the diretlink to the project area and the name. So, i need these both links:

<jp06:url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw</jp06:url>
or
-<jp06:project-area jp06:name="QM1">

I tried to realise it with calling the classes in the xml-file:

NodeList projectarea = doc.getElementsByTagName("jp06:url");
for(int x=0,size= projectarea.getLength(); x<size; x++) { System.out.println(projectarea.item(x).getNodeValue( ) ); 
}


But the result is always null. I am not sure how does it work with this kind of structure of XML-File?

In this Example/XML-File it works with no problems:

<Item type="Text" name="Family" id="c">350</Item>

In Java: ....


NodeList projectarea = doc.getElementsByTagName("Item");
for(int x=0,size= projectarea.getLength(); x<size; x++) System.out.println(projectarea.item(x).getAttributes().getNamedItem("name").getNodeValue()); 
}


Can someone help me to realise it with the responseBody-xml?
And what does "jp06:..." mean? is "jp06" always the same and similar to ccm,rm,qm?


Thank you very much!


3 answers



permanent link
Reshma Ratnani (1.1k1) | answered Jan 13 '16, 12:34 a.m.
JAZZ DEVELOPER
Please search for Parsing XML document on internet for correct code to get the node and attribute values

Comments
Bastian W commented Jan 13 '16, 4:35 a.m.

What do you mean? Do you mean, that my "responseBody" isn't a xml-object now?


permanent link
Bastian W (217) | answered Jan 13 '16, 4:57 a.m.
edited Jan 13 '16, 5:00 a.m.

If i save my parsed String, i will get a .xml file like this one.


< ?xml version="1.0" encoding="UTF-8"? >
<jp06:project-areas xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/">
    <jp06:project-area jp06:name="QM1">
        <jp06:summary>REST CALLS</jp06:summary>
        <jp06:description>This is a test</jp06:description>
        <jp06:url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw</jp06:url>
        <jp06:roles-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/roles</jp06:roles-url>
        <jp06:links-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/links</jp06:links-url>
        <jp06:members-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/members</jp06:members-url>
        <jp:admins-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/admins</jp:admins-url>
        <jp06:team-areas-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/team-areas</jp06:team-areas-url>
        <jp06:project-admins-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/project-admins</jp06:project-admins-url>
        <jp06:read-access-list-url>https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/read-access-list</jp06:read-access-list-url>
        <jp:timelines-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/timelines</jp:timelines-url>
        <jp06:access-public>false</jp06:access-public>
        <jp06:access-visible-to-access-list>false</jp06:access-visible-to-access-list>
        <jp06:access-visible-to-members>true</jp06:access-visible-to-members>
        <jp06:modified>Mon, 11 Jan 2016 11:30:11 GMT</jp06:modified>
        <jp06:archived>false</jp06:archived>
        <jp:history-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_CJ0oEHDKEeWTw5QohIqrPw/history</jp:history-url>
    </jp06:project-area>
    <jp06:project-area jp06:name="LifeCycleProject1 (Quality Management)">
        <jp06:summary>LifeCycleProject1 (Quality Management)</jp06:summary>
        <jp06:description/>
        <jp06:url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw</jp06:url>
        <jp06:roles-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/roles</jp06:roles-url>
        <jp06:links-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/links</jp06:links-url>
        <jp06:members-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/members</jp06:members-url>
        <jp:admins-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/admins</jp:admins-url>
        <jp06:team-areas-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/team-areas</jp06:team-areas-url>
        <jp06:project-admins-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/project-admins</jp06:project-admins-url>
        <jp06:read-access-list-url>https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/read-access-list</jp06:read-access-list-url>
        <jp:timelines-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/timelines</jp:timelines-url>
        <jp06:access-public>false</jp06:access-public>
        <jp06:access-visible-to-access-list>false</jp06:access-visible-to-access-list>
        <jp06:access-visible-to-members>true</jp06:access-visible-to-members>
        <jp06:modified>Mon, 12 Oct 2015 11:10:29 GMT</jp06:modified>
        <jp06:archived>false</jp06:archived>
        <jp:history-url xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/">https://localhost:9443/qm/process/project-areas/_xm3ScHDREeWTw5QohIqrPw/history</jp:history-url>
    </jp06:project-area>
</jp06:project-areas>


I think, it's the correct one. But i can't identify the attributes now and have problems with the "jp06"


permanent link
Bastian W (217) | answered Jan 13 '16, 6:52 a.m.
OK, i have the solution, it was everything right =)


String responseBody = httpclient.execute(httpget, responseHandler);   
                       
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
                       
                         
builder = factory.newDocumentBuilder(); 
Document doc = builder.parse( new InputSource( new StringReader(responseBody)));                                

NodeList projectarea = doc.getElementsByTagName("jp06:url");

System.out.println(projectarea.getLength());
                               
for(int x=0,size= projectarea.getLength(); x<size; x++) { 

//Here was the mistake! Thats the correct one!                                
System.out.println(projectarea.item(x).getFirstChild().getTextContent());
}


Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.