2016-12-23 11 views
0

私はこれを使用しています:Openfire Rest APIopenfire rest APIのrestclient.entity.UserEntitiesからデータを抽出する方法は?

私はjavaファイルを使用してユーザーとグループを取得しています。 私はXMLデータを期待していましたが、それは私に奇妙なデータを示しています。

私はJavaを初めて使用しています。そのため、これからデータを抽出する方法はわかりません。

私のコードは次のとおりです。

package bizrtc; 

import api.restclient.RestClient; 
import api.restclient.RestApiClient; 
import api.restclient.entity.AuthenticationToken; 
import api.restclient.entity.UserEntities; 
import api.restclient.entity.GroupEntities; 


import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 


/** 
* 
* @author Rajan 
*/ 
public class Bizrtc 
{ 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     // TODO code application logic here 
     AuthenticationToken authenticationToken = new AuthenticationToken("cn1ed9s8yEf5woQV"); 
     // Set Openfire settings (9090 is the port of Openfire Admin Console) 
     RestApiClient restApiClient = new RestApiClient("192.168.50.50", 9090, authenticationToken); 
     // restApiClient.getUsers(); 
     UserEntities users = restApiClient.getUsers(); 

     System.out.println("The Groups are as below: "+restApiClient.getGroups()); 

     System.out.println("Now fetching data from openfire Server"); 
     System.out.println("The data is *******************************" + users.toString()); 

    } 



} 

そして、私は私が手にプログラムを実行します。

Dec 23, 2016 3:58:43 PM org.glassfish.jersey.filter.LoggingFilter log 
INFO: 1 * Sending client request on thread main 
1 > GET http://192.168.50.50:9090/plugins/restapi/v1/groups 
1 > Authorization: cn1ed9s8yEf5woQV 
1 > Content-Type: application/xml 

Dec 23, 2016 3:58:44 PM org.glassfish.jersey.filter.LoggingFilter log 
INFO: 1 * Client response received on thread main 
1 < 200 
1 < Access-Control-Allow-Credentials: true 
1 < Access-Control-Allow-Headers: origin, content-type, accept, authorization 
1 < Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD 
1 < Access-Control-Allow-Origin: * 
1 < Content-Length: 3664 
1 < Content-Type: application/xml 
1 < Date: Fri, 23 Dec 2016 09:53:38 GMT 
1 < Expires: Thu, 01 Jan 1970 00:00:00 GMT 
1 < Set-Cookie: JSESSIONID=1bt213yrejbmfkpyfs53snplm;Path=/;HttpOnly 
1 < X-Frame-Options: deny 

The Groups are as below: [email protected] 
Now fetching data from openfire Server 
The data is *******************************[email protected] 

ARRAY形式にXML以上にこのユーザーを印刷する方法を?

は、どのように私はこの応答からユーザーを取得します:[email protected]

iは、文字列、またはそのような何かにこれを変換する必要がありますか?

答えて

1

UserEntities Javaコードを見てみましょう:

@XmlRootElement(
    name = "users" 
) 
public class UserEntities { 
    List<UserEntity> users; 

    public UserEntities() { 
    } 

    public UserEntities(List<UserEntity> users) { 
     this.users = users; 
    } 

    @XmlElement(
     name = "user" 
    ) 
    public List<UserEntity> getUsers() { 
     return this.users; 
    } 

    public void setUsers(List<UserEntity> users) { 
     this.users = users; 
    } 
} 

それはユーザーのリストを持つ単一のPOJOクラスだとJAXB注釈にマッピングされました。つまり、オブジェクトをXML、JSONなどに簡単に変換できます。

XML方法:

JAXBContext jaxbContext = JAXBContext.newInstance(UserEntities.class); 
Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

StringWriter sw = new StringWriter(); 
jaxbMarshaller.marshal(users, sw); 
String xmlString = sw.toString(); 

System.out.println(xmlString); 

そして、あなたはUserEntity年代の配列をしたい場合、あなたはすでにそのListあります

final UserEntity[] arrayUsers = (UserEntity[]) users.getUsers().toArray(); 

リターン例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<users> 
    <user> 
     <username>d</username> 
     <name>e</name> 
     <email>[email protected]</email> 
     <password>pass</password> 
    </user> 
</users> 
関連する問題