Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Get Attributes from XML-File (ResponseBody) in Java

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!


0 votes



3 answers

Permanent link
Please search for Parsing XML document on internet for correct code to get the node and attribute values

0 votes

Comments

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


Permanent link

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"

0 votes


Permanent link
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());
}


0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: Jan 11 '16, 8:04 a.m.

Question was seen: 2,379 times

Last updated: Jan 13 '16, 6:52 a.m.

Confirmation Cancel Confirm