Reputation: 23
How to parse xml file from this code. Code is working from URL but what i need to parse xml file from sdcard. I add premission to write and read from sdcard.
Code :
menuItems = new ArrayList<HashMap<String, String>>();
final XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));
// adding HashList to ArrayList
menuItems.add(map);
}
XMLClass:
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(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) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
What i need edit? Location of folder on sdcard is "/sdcard/.Folder/". Can I add that code on XMLClass?
Upvotes: 0
Views: 1990
Reputation: 33495
What i need edit? Location of folder on sdcard is "/sdcard/.Folder/". Can I add that code on XMLClass?
Here is my first idea.
Since your source file is located in mobile phone, you can remove all your work with HttpClient
because you don't need it. Then you need to read your file and assign its content into String. I recommend for an efficiency to use StringBuffer for instance. For getting path to external storage you should use Enviroment class that provides proper method.
Pseudo-code:
public String getXmlFromFile(String filename) {
StringBuffer buff = new StringBuffer();
File root = Environment.getExternalStorageDirectory();
File xml = new File(root, "pathToYourFile");
BufferedReader reader = new BufferedReader(new FileReader(xml));
String line = null;
while ((line = reader.readLine()) != null) {
buff.append(line).append("\n");
}
reader.close();
return buff.toString();
}
And now you have your XML
String in memory and you can pass it to getDomElement(String xml)
method.
Also you can directly assign your File
into DocumentBuilder
Pseudo-code:
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File root = Environment.getExternalStorageDirectory();
File xmlFile = new File(root, "pathToYourFile");
doc = builder.parse(xmlFile);
return doc;
You can use as parameter of parse()
function raw InputStream
as well.
Upvotes: 1
Reputation: 644
You can use XmlPullParser
class and .setInput(bufferedReader)
method to read a file. Path get via Environment
class.
Upvotes: 0