hi guys ,
android web services is very important. sending data to web server and receiving data to web server importance. we are using few parsing techniques.
That's are:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
try {
URL url = new URL( "your url ");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("results");// chance to your tag name
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("output");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("results = "
+ ((Node) nameList.item(0)).getNodeValue());
layout.addView(name[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}
android web services is very important. sending data to web server and receiving data to web server importance. we are using few parsing techniques.
That's are:
1. DOM PARSER.
2. SAX PARSER.
3. XML PULL PARSER.
DOM PARSER:
DOM (Document Object Model) parsing builds an in-memory object representation of the entire XML document.
You can then rummage around in the DOM, going and back and forth between elements and reading data from them in whatever order you like.
Because the entire document is read into memory, there is an upper limit on the size of document you can read (constrained by the size of your Java heap).
Memory is not used particularly efficiently either - a DOM may consist of very many sparsely populated
List
objects (backed by mostly empty arrays).
A side effect of all these objects in memory is that when you're finished with them there's a lot for the Garbage Collector to clean up.
On the plus side, DOM parsing is straight-forward to work with, particularly if you don't care much about speed and use
getElementsByTagName()
wherever possible.
DOM Features:
- Stores the entire XML document into memory before processing.
- Occupies more memory.
- We can insert or delete nodes.
- Traverse in any direction.
some point:
A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.
· A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.
· A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.
SAMPLE CODE FOR HOW TO PARSING TO XML:
public class domparser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
try {
URL url = new URL( "your url ");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("results");// chance to your tag name
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("output");
// chance to your sub tag name
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("results = "
+ ((Node) nameList.item(0)).getNodeValue());
layout.addView(name[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}
The xml source code is:
<result>
<output>successfully received</output>
</result>
No comments:
Post a Comment