How to print the object value of XML file in String format(first in the log cat file later onto the console)



This is not a duplicate to earlier post as in that post I was unable to get the p list file but now I am able to retrieve the p-list file .I have earlier asked the question on converting the object value into String format


I have followed this site in order to retrieve the p-list file along with some modifications. Initially I want to print the contents of the file in the logcat. Once I am able to print the contents(just to make sure whether the file retrieved is the same from the assets) then I will focus on displaying the contents on the Console of the emulator



package com.cmc.readlist2;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class ParseList {

// parse Plist and fill in arraylist

public ArrayList<DataModel> parsePlist(String xml) {
final ArrayList<DataModel> dataModels = new ArrayList<DataModel>();
// System.out.println("fff");
//Get the xml string from assets
final Document doc = XMLfromString(xml);
final NodeList nodes_array = doc.getElementsByTagName("array");

//Fill in the list items from the XML document
for ( int index = 0; index < nodes_array.getLength(); index++ ) {

final Node node = nodes_array.item(index);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {

final Element e = (Element)nodes_array.item(index);
final NodeList nodeKey = e.getElementsByTagName("key");
System.out.println(" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+nodeKey.item(1).toString());
final NodeList nodeValue = e.getElementsByTagName("string");
DataModel model = new DataModel();
for (int i=0; i < nodeValue.getLength(); i++) {
System.out.println("Value of the node is " + nodeValue.toString());

System.out.println();
final Element eleKey = (Element)nodeKey.item(i);
System.out.println("Value of Element is " +eleKey);
final Element eleString = (Element)nodeValue.item(i);
if ( eleString != null ) {

String strValue = getValue(eleString, "string");
if(getValue(eleKey, "key").equals("Header")) {

model = new DataModel();
model.setHeader(Integer.parseInt(strValue));

} else if(getValue(eleKey, "key").equals("details")) {

model.setdetails(Integer.parseInt(strValue));

if ( strValue == null ) {
strValue = "";
}

dataModels.add(model);
}
}

}

}

}
return dataModels;
}
// Create xml document object from XML String
private Document XMLfromString(String xml) {
Document doc = null;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {

System.out.println("XML parse error: " + e.getMessage());
return null;
}catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
}catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
// fetch value from Text Node only
private String getElementValue(Node elem) {
Node kid;
if (elem != null) {
if (elem.hasChildNodes()) {
for (kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
if (kid.getNodeType() == Node.TEXT_NODE) {
return kid.getNodeValue();
}
}
}
}
return "";
}
/// Fetch value from XML Node

public String getValue(Element item, String str) {

NodeList n = item.getElementsByTagName(str);
return getElementValue(n.item(0));
}

}


Below is my Main Activity:-



package com.cmc.readlist2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

//import android.R.*;
import android.util.Log;

import com.cmc.plistparser.java.R;
import com.cmc.readlist2.DataModel;
import com.cmc.readlist2.ParseList;

import android.app.Activity;
//import android.content.Intent;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class PlistParser extends Activity implements OnClickListener {

Button btnStart;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plist_parser);
btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);
}

// Read plist from Assets

public String readPlistFromAssets() {

StringBuffer sb = new StringBuffer();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("FAQFile.plist")));
String temp=null;

while ((temp = br.readLine()) != null){
sb.append(temp);
}
//System.out.println(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
br.close(); // stop reading
} catch (IOException ex) {
ex.printStackTrace();
}

}
return sb.toString();

}//method close

// Start Parsing on Button Click

public void onClick(View v) {

// Read Plist content

String xml = readPlistFromAssets();

// create object of Parser Class

ParseList pp = new ParseList();

// parse Plist and fill in plist array
ArrayList<DataModel> plist = pp.parsePlist(xml);

// Print ArrayList

Iterator<DataModel> i = plist.iterator();

while(i.hasNext()){

DataModel d = i.next();
//
// Log.i("Plist","=========================");
// Log.i("Plist","Header " + d.getHeader());
// Log.i("Plist","Details " + d.getdetails());
// Log.i("Plist","Header********************************* " + d.toString());
//
}
Toast.makeText(this, "Parsing has been completed please check logcat...", Toast.LENGTH_LONG).show();

}

}


Below is my LogCat File:-



I/System.out(919): $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$org.apache.harmony.xml.dom.ElementImpl@b2dd54a0
I/System.out(919): Value of the node is org.apache.harmony.xml.dom.NodeListImpl@b2dd6c20
I/System.out(919): Value of Element is org.apache.harmony.xml.dom.ElementImpl@b2dd4fb0
I/System.out(919): Value of the node is org.apache.harmony.xml.dom.NodeListImpl@b2dd6c20
I/System.out(919): Value of Element is org.apache.harmony.xml.dom.ElementImpl@b2dd54a0


I also want to print the contents of plist.xml from assets folder(In the form of header ,details in string format) into the Console of my Emulator. So is there any way that could be done.


No comments:

Post a Comment