2012-03-26 63 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

相关问题