0

I have an XML file to parse

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<WiFi>
<SSID>testwifi</SSID>
<Key>testkey</Key>
</WiFi>
<Contact>
BEGIN:VCARD
VERSION:2.1
N:;AL entertainment;;;
FN:AL entertainment
TEL;CELL;PREF:543-212
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest;;;
FN:Forrest Gump
TEL;CELL;PREF:543-404
EMAIL;PREF:satvinder94@gmail.com
END:VCARD
</Contact>
</Response>

I am parsing it using below code.

public boolean getXmlFromUrl() {

    boolean status = false;

    StringBuffer output = new StringBuffer("");

    try {

        InputStream stream = null;

        URL url = new URL("uploaded url to test server URL");

        URLConnection connection = url.openConnection();

        HttpURLConnection httpConnection = (HttpURLConnection)connection;

        httpConnection.setRequestMethod("GET");

        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream()
            BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));

            String s = "";

            while ((s = buffer.readLine()) != null

                output.append(s);

            status = true;
        }

        mScanXMLStr = output.toString();
        Log.d(TAG,"string :: "+mScanXMLStr);
    } catch (Exception ex) {

        ex.printStackTrace();
    }
    return status;
}

Ho can I preserve new line in contact tag i.e. from BEGIN:VCARD to END:VCARD?

gengisdave
  • 1,969
  • 5
  • 21
  • 22

2 Answers2

1

First, you should replace output.append(s) with output.append(s + \n), it will read from stream with new line symbols. And yes, you do not parse, you only read. If you want to parse, I odder to create two classes:

public class XmlResponseParser{

    public static Response parse(String s){
        Response response = new Response();

        //as <SSID> and <Key> are inner parameters of <WiFi>, they should be          
        //looked for inside <WiFi> tag
        String wifiString = findTagValue(s, "WiFi");
        response.wifi.ssid = findTagValue(wifiString, "SSID");
        response.wifi.key = findTagValue(wifiString, "Key");
        response.contact = findTagValue(s, "Contact");

        return response;
    }
    //finds and returns value of tag in substring
    private static String findTagValue(String s, String tag){

        int startTagValueIndex = s.indexOf("<" + tag + ">") + tag.length() + 2;
        int endTagValueIndex = s.indexOf("</" + tag + ">");

        return s.substring(startTagValueIndex, endTagValueIndex);
    }

}

and

public class Response{
    WiFi wifi;
    String contact;

    public Response(){
        wifi = new WiFi();
    }


    public class WiFi{
        String ssid;
        String key;
    }
}

Then you can call XmlResponseParser.parse(mScanXMLStr) in your code and it will return you and instance of Response class.

EDIT

Also, take a look at SimpleXml library to parse XML

0

Try using &#xA instead of new line character

there is a similar answer here: Preserve newlines when parsing xml

Community
  • 1
  • 1
Nikita
  • 77
  • 10