2011-03-03 66 views
0

我正在開發一個android應用程序,在那裏我從使用SOAP的.NET web服務中獲取一些對象。我沒有問題得到我的對象。我使用kso​​ap2-android來處理請求。在Java中解析未知對象

我需要一個很好的方法來解析這個對象的類型到一個互補的Java對象。任何人都知道這樣做的好方法?

它由簡單類型和由簡單類型組成的其他對象組成。

我現在知道的唯一方法,是一樣的東西:

String[] types = o.toString().split(";"); 

然後解析字符串數組。大部分有更方便的方法來做到這一點?

OK的例子是一個好主意,所以這裏是我的對象之一:

anyType的{成功=真; UserMessage = anyType {}; TechnicalMessage = anyType {}; IntValue = 0;的doubleValue = 0; DateTimeValue = 0001-01-01T00:00:00; AgeGroups = anyType {}; SessionConfigurations = anyType {Client_SessionConfiguration = anyType {ID = 2; Name = Skoleklasser; }; Client_SessionConfiguration = anyType {ID = 3; Name = Virksomheder; }; Client_SessionConfiguration = anyType {ID = 4; Name =Gæster; }; }; }

+2

在迂腐的筆記上,不是所有的對象都由簡單類型和其他對象組成嗎? – Armand 2011-03-03 14:33:55

+0

Alison:是的...... – DNRN 2011-03-04 10:01:54

回答

1

我從android調用了很多web服務。我擴展org.xml.sax.helpers.DefaultHandler類來處理響應。

public class BaseXMLResponseHandler extends DefaultHandler { 

    StringBuffer accumulator = new StringBuffer(); 


    @Override 
    public void characters(char[] buffer, int start, int length) 
      throws SAXException { 
      accumulator.append(buffer, start, length); 
    } 
} 

然後,只需執行方法

public void endElement(String uri, String localName, String qName) throws SAXException 
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 

例如:

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
accumulator.setLength(0); // Ready to accumulate new text 
} 

List<String> testElements = new ArrayList<String>(); 
    public void endElement(String uri, String localName, String qName) throws SAXException 
{ 

      if (localName.equals("testelement")) { 
        testElements.add(accumulator.toString()); 
       } 
    } 

    List<String> getElements() { 
    return testElements; 
    } 
+0

我使用kSoap-android來處理SOAP ......我的問題只是找到一種簡單的方法來在Java中序列化我的對象。 – DNRN 2011-03-07 15:59:27

1

你問「如何序列化和反序列化的Java對象?」

嘗試JSON

Google提供了一個名爲gson的良好JSON解析庫。

這個問題的答案很大程度上取決於你的對象在;如何張貼樣本?

+0

其他選項:Google Protobuffs和XML。 – 2011-03-03 15:27:20

+0

我在我的文章中包含了一個對象的例子。希望這是正確的做法嗎?而不是張貼另一個。 – DNRN 2011-03-04 10:05:55

+0

不幸的是我不知道使用kso​​ap2。儘管我在網上搜索時看到了很多教程。 – 2011-03-04 15:32:00