2012-03-26 89 views
4

您好,我正在製作預訂應用程序,並且需要在創建xml後將xml發送到服務器。如何在android應用程序中使用xmlserializer創建xml

如何使用xmlserializer創建XML,它創建後發送到服務器?

http://api.ean.com/ean-services/rs/hotel/v3/list? 
minorRev=[current minorRev #] 
&cid=55505 
&apiKey=[xxx-yourOwnKey-xxx] 
&customerUserAgent=[xxx]&customerIpAddress=[xxx] 
&locale=en_US 
&currencyCode=USD 
&xml= 
    <HotelListRequest> 
<city>Seattle</city> 
<stateProvinceCode>WA</stateProvinceCode> 
<countryCode>US</countryCode> 
<arrivalDate>08/01/2012</arrivalDate> 
<departureDate>08/03/2012</departureDate> 
<RoomGroup> 
    <Room> 
    <numberOfAdults>2</numberOfAdults> 
    </Room> 
</RoomGroup> 
<numberOfResults>1</numberOfResults> 
    <supplierCacheTolerance>MED_ENHANCED</supplierCacheTolerance> 
    </HotelListRequest> 

回答

1

傢伙看到這個源

package com.ex.createXml; 

import android.app.Activity; 
import android.os.Bundle; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import org.xmlpull.v1.XmlSerializer; 
import android.os.Environment; 
import android.util.Log; 
import android.util.Xml; 
import android.widget.TextView; 

public class createXml extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

File newxmlfile = new File("C:/new.xml"); 
try { 
    newxmlfile.createNewFile(); 
} catch (IOException e) { 
    Log.e("IOException", "Exception in create new File("); 
} 

FileOutputStream fileos = null; 
try{ 
    fileos = new FileOutputStream(newxmlfile); 

} catch(FileNotFoundException e) { 
    Log.e("FileNotFoundException",e.toString()); 
} 
XmlSerializer serializer = Xml.newSerializer(); 

try { 
    serializer.setOutput(fileos, "UTF-8"); 
    serializer.startDocument(null, Boolean.valueOf(true)); 
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 
    serializer.startTag(null, "root"); 
    serializer.startTag(null, "Child1"); 
    serializer.endTag(null, "Child1"); 
    serializer.startTag(null, "Child2"); 
    serializer.attribute(null, "attribute", "value"); 
    serializer.endTag(null, "Child2"); 
    serializer.startTag(null, "Child3"); 
    serializer.text("Some text inside child 3"); 
    serializer.endTag(null,"Child3"); 
    serializer.endTag(null,"root"); 
    serializer.endDocument(); 
    serializer.flush(); 
    fileos.close(); 
    //TextView tv = (TextView)findViewById(R.); 

} catch(Exception e) { 
    Log.e("Exception","Exception occured in wroting"); 
} 
    } } 
+0

此代碼的結果是一個文件,但我需要在創建xml後將xml發送到服務器。 – vinuonline 2012-03-26 09:58:59

+0

我認爲你可以讀取文件併發送它,但最好的方法是使用內存流將其放在內存中,然後讀取內存併發送它。 – Peyman 2012-10-22 17:29:58

18

你需要爲字符串輸出創建作家。

@SuppressWarnings("null") 
public static String CreateXMLString() throws IllegalArgumentException, IllegalStateException, IOException 
{ 
    XmlSerializer xmlSerializer = Xml.newSerializer(); 
     StringWriter writer = new StringWriter(); 

    xmlSerializer.setOutput(writer); 

    //Start Document 
    xmlSerializer.startDocument("UTF-8", true); 
    xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 
    //Open Tag <file> 
    xmlSerializer.startTag("", "file"); 

    xmlSerializer.startTag("", "something"); 
    xmlSerializer.attribute("", "ID", "000001"); 

    xmlSerializer.startTag("", "name"); 
    xmlSerializer.text("CO"); 
    xmlSerializer.endTag("", "name"); 

    xmlSerializer.endTag("", "something"); 



    //end tag <file> 
    xmlSerializer.endTag("", "file"); 
    xmlSerializer.endDocument(); 

    return writer.toString(); 
} 

和輸出字符串是這樣的:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
    <file> 
    <something ID="000001"> 
    <name> 
    CO 
    </name> 
    </something> 
    </file> 

但我不知道如何發送it.Maybe,您可以在此字符串轉換爲字節。

+0

有沒有什麼辦法可以在「startDocument」中放入不同的2.0版本? – ikzjfr0 2014-10-15 03:54:43

+0

也許有,但你爲什麼需要不同的版本? – COvayurt 2014-10-16 06:44:31

+0

我的問題聽起來很愚蠢,但XmlSerializer是否意味着將對象轉換爲字節流的數據序列化?因爲現在我有輸出字符串,但不是字節流本身! – 2016-12-20 11:48:24

相關問題