如何在Android应用程序中使用xmlserializer创建一个xml

时间:2012-03-26 09:19:41

标签: android xml xmlserializer

您正在制作预订应用,我需要在创建xml后向服务器发送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>

2 个答案:

答案 0 :(得分:19)

您需要为字符串输出创建编写器。

@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>

但我不知道如何发送它。也许,你可以把这个字符串转换成字节。

答案 1 :(得分: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");
}
   } }