Gelöschter Account
- Forum-Beiträge: 37
21.09.2012, 20:04:11 via Website
21.09.2012 20:04:11 via Website
Hallo,
ich habe von hier: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ das XML Beispiel angeschaut.
Doch ich weiß nicht das das e in folgenden Zeilen sein soll:
e = Typ Element
Aber woher bekomm ich das...ich blick da gerade nicht so durch...Kann mir da jemand Helfen?
Hier die XML Classe:
Und hier der Aufruf von mir:
ich habe von hier: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ das XML Beispiel angeschaut.
Doch ich weiß nicht das das e in folgenden Zeilen sein soll:
1String name = parser.getValue(e, KEY_NAME); // name child value
2 String cost = parser.getValue(e, KEY_COST); // cost child value
3 String description = parser.getValue(e, KEY_DESC); // description child value
2 String cost = parser.getValue(e, KEY_COST); // cost child value
3 String description = parser.getValue(e, KEY_DESC); // description child value
e = Typ Element
Aber woher bekomm ich das...ich blick da gerade nicht so durch...Kann mir da jemand Helfen?
Hier die XML Classe:
1package com.test.test;
2
3import java.io.IOException;
4import java.io.StringReader;
5import java.io.UnsupportedEncodingException;
6
7import javax.xml.parsers.DocumentBuilder;
8import javax.xml.parsers.DocumentBuilderFactory;
9import javax.xml.parsers.ParserConfigurationException;
10
11import org.apache.http.HttpEntity;
12import org.apache.http.HttpResponse;
13import org.apache.http.client.ClientProtocolException;
14import org.apache.http.client.methods.HttpPost;
15import org.apache.http.impl.client.DefaultHttpClient;
16import org.apache.http.util.EntityUtils;
17import org.w3c.dom.Document;
18import org.w3c.dom.Element;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21import org.xml.sax.InputSource;
22import org.xml.sax.SAXException;
23
24import android.util.Log;
25
26public class XMLParser {
27
28 public String getXmlFromUrl(String url) {
29 String xml = null;
30
31 try {
32 // defaultHttpClient
33 DefaultHttpClient httpClient = new DefaultHttpClient();
34 HttpPost httpPost = new HttpPost(url);
35
36 HttpResponse httpResponse = httpClient.execute(httpPost);
37 HttpEntity httpEntity = httpResponse.getEntity();
38 xml = EntityUtils.toString(httpEntity);
39
40 } catch (UnsupportedEncodingException e) {
41 e.printStackTrace();
42 } catch (ClientProtocolException e) {
43 e.printStackTrace();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 // return XML
48 return xml;
49 }
50
51 //XML Parsing
52 public Document getDomElement(String xml){
53 Document doc = null;
54 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
55 try {
56
57 DocumentBuilder db = dbf.newDocumentBuilder();
58
59 InputSource is = new InputSource();
60 is.setCharacterStream(new StringReader(xml));
61 doc = db.parse(is);
62
63 } catch (ParserConfigurationException e) {
64 Log.e("Error: ", e.getMessage());
65 return null;
66 } catch (SAXException e) {
67 Log.e("Error: ", e.getMessage());
68 return null;
69 } catch (IOException e) {
70 Log.e("Error: ", e.getMessage());
71 return null;
72 }
73 // return DOM
74 return doc;
75 }
76
77 public String getValue(Element item, String str) {
78 NodeList n = item.getElementsByTagName(str);
79 return this.getElementValue(n.item(0));
80 }
81
82 public final String getElementValue( Node elem ) {
83 Node child;
84 if( elem != null){
85 if (elem.hasChildNodes()){
86 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
87 if( child.getNodeType() == Node.TEXT_NODE ){
88 return child.getNodeValue();
89 }
90 }
91 }
92 }
93 return "";
94 }
95
96}
2
3import java.io.IOException;
4import java.io.StringReader;
5import java.io.UnsupportedEncodingException;
6
7import javax.xml.parsers.DocumentBuilder;
8import javax.xml.parsers.DocumentBuilderFactory;
9import javax.xml.parsers.ParserConfigurationException;
10
11import org.apache.http.HttpEntity;
12import org.apache.http.HttpResponse;
13import org.apache.http.client.ClientProtocolException;
14import org.apache.http.client.methods.HttpPost;
15import org.apache.http.impl.client.DefaultHttpClient;
16import org.apache.http.util.EntityUtils;
17import org.w3c.dom.Document;
18import org.w3c.dom.Element;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21import org.xml.sax.InputSource;
22import org.xml.sax.SAXException;
23
24import android.util.Log;
25
26public class XMLParser {
27
28 public String getXmlFromUrl(String url) {
29 String xml = null;
30
31 try {
32 // defaultHttpClient
33 DefaultHttpClient httpClient = new DefaultHttpClient();
34 HttpPost httpPost = new HttpPost(url);
35
36 HttpResponse httpResponse = httpClient.execute(httpPost);
37 HttpEntity httpEntity = httpResponse.getEntity();
38 xml = EntityUtils.toString(httpEntity);
39
40 } catch (UnsupportedEncodingException e) {
41 e.printStackTrace();
42 } catch (ClientProtocolException e) {
43 e.printStackTrace();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 // return XML
48 return xml;
49 }
50
51 //XML Parsing
52 public Document getDomElement(String xml){
53 Document doc = null;
54 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
55 try {
56
57 DocumentBuilder db = dbf.newDocumentBuilder();
58
59 InputSource is = new InputSource();
60 is.setCharacterStream(new StringReader(xml));
61 doc = db.parse(is);
62
63 } catch (ParserConfigurationException e) {
64 Log.e("Error: ", e.getMessage());
65 return null;
66 } catch (SAXException e) {
67 Log.e("Error: ", e.getMessage());
68 return null;
69 } catch (IOException e) {
70 Log.e("Error: ", e.getMessage());
71 return null;
72 }
73 // return DOM
74 return doc;
75 }
76
77 public String getValue(Element item, String str) {
78 NodeList n = item.getElementsByTagName(str);
79 return this.getElementValue(n.item(0));
80 }
81
82 public final String getElementValue( Node elem ) {
83 Node child;
84 if( elem != null){
85 if (elem.hasChildNodes()){
86 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
87 if( child.getNodeType() == Node.TEXT_NODE ){
88 return child.getNodeValue();
89 }
90 }
91 }
92 }
93 return "";
94 }
95
96}
Und hier der Aufruf von mir:
1XMLParser parser = new XMLParser();
2 String xml = parser.getXmlFromUrl("http://www.domain.de/test.xml"); // getting XML
3 Document doc = parser.getDomElement(xml); // getting DOM element
4
5 NodeList nl = doc.getElementsByTagName(KEY_ITEM);
6
7 // looping through all item nodes <item>
8 for (int i = 0; i < nl.getLength(); i++) {
9
10 String name = parser.getValue(e, KEY_NAME); // name child value
11 String cost = parser.getValue(e, KEY_COST); // cost child value
12 String description = parser.getValue(e, KEY_DESC); // description child value
13 }
2 String xml = parser.getXmlFromUrl("http://www.domain.de/test.xml"); // getting XML
3 Document doc = parser.getDomElement(xml); // getting DOM element
4
5 NodeList nl = doc.getElementsByTagName(KEY_ITEM);
6
7 // looping through all item nodes <item>
8 for (int i = 0; i < nl.getLength(); i++) {
9
10 String name = parser.getValue(e, KEY_NAME); // name child value
11 String cost = parser.getValue(e, KEY_COST); // cost child value
12 String description = parser.getValue(e, KEY_DESC); // description child value
13 }
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.