how to create an xml using xmlserializer in android app

坚强是说给别人听的谎言 提交于 2019-12-06 04:24:02

问题


Hi am making a booking app and i need to send an xml to the server after creating the xml.

How to create the xml using xmlserializer and send it to a server after creating it?

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:


You need to create writer for string output.

@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();
}

And the output string is like this:

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

But i don't know how to send it.Maybe, you can convert this string into bytes.




回答2:


dude see this source

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");
}
   } }


来源:https://stackoverflow.com/questions/9869507/how-to-create-an-xml-using-xmlserializer-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!