6

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>
Luca Filosofi
  • 30,905
  • 9
  • 70
  • 77
vinuonline
  • 143
  • 2
  • 3
  • 14

2 Answers2

21

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.

fmt.Println.MKO
  • 2,065
  • 1
  • 17
  • 25
COvayurt
  • 827
  • 2
  • 11
  • 36
  • Is there any way to put a different version like 2.0 in "startDocument"? – ikzjfr0 Oct 15 '14 at 03:54
  • Maybe there is but why do you need different version ? – COvayurt Oct 16 '14 at 06:44
  • My question might sound silly, but does XmlSerializer means data serialization where objects are converted into byte streams ? Because now I have the output string but not the byte stream per-se !! – McLan Dec 20 '16 at 11:48
1

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");
}
   } }
halfer
  • 19,824
  • 17
  • 99
  • 186
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
  • the result for this code is a file, but i need the xml to send to a server after creating the xml. – vinuonline Mar 26 '12 at 09:58
  • I think you can read the file and sent it but the best way is to put it on the memory using Memory stream and then read the memory and send it. – Peyman Oct 22 '12 at 17:29