(Chilkat for Android™ API) Methods for Getting Attributes
Demonstrates some methods for getting attribute name/values.
The input XML, available at http://www.chilkatsoft.com/data/car.xml, is this:
<root>
<car color="black" make="mercedes" model="C350" hp="302" engine="v6" type="sedan">Mercedes Benz C350</car>
</root>
Download: Chilkat for Android™ Java Libraries
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String outStr = "";
CkXml xml = new CkXml();
CkXml carNode;
int numAttr;
int horsepower;
int i;
boolean success;
// The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
success = xml.LoadXmlFile("car.xml");
if (success != true) {
outStr += xml.lastErrorText() + "\n";
tv.setText(outStr);
setContentView(tv);
return;
}
// Navigate to the "car" node, which is the 1st child:
carNode = xml.FirstChild();
// Get the value of the "model" attribute:
outStr += "model = " + carNode.getAttrValue("model") + "\n";
// Get the value of the "hp" attribute as an integer:
horsepower = carNode.GetAttrValueInt("hp");
outStr += "horsepower = " + horsepower + "\n";
// Iterate over the attributes and show the name/value of each:
numAttr = carNode.get_NumAttributes();
i = 0;
while (i < numAttr) {
outStr += carNode.getAttributeName(i)
+ ": " + carNode.getAttributeValue(i) + "\n";
i = i + 1;
}
tv.setText(outStr);
setContentView(tv);
}
static {
// Important: Make sure the name passed to loadLibrary matches the shared library
// found in your project's libs/armeabi directory.
// for "libchilkat.so", pass "chilkat" to loadLibrary
// for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
// etc.
//
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}
|