Is there a short way to parse XML string in Android
Try this,
public void parseXml(){
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader( "<foo>Hello World!</foo>" ) ); // pass input whatever xml you have
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
Log.d(TAG,"Start document");
} else if(eventType == XmlPullParser.START_TAG) {
Log.d(TAG,"Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
Log.d(TAG,"End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
Log.d(TAG,"Text "+xpp.getText()); // here you get the text from xml
}
eventType = xpp.next();
}
Log.d(TAG,"End document");
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
How to parse an xml file in android?
I found a way to access the attribute values. I have explained all the lines in the comments.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file); // We can use inputstream or uri also
NodeList node = doc.getElementsByTagName("rootfile"); // gets list of all nodes by specified tag name.
Node map = node.item(0); // Getting the specific node
NamedNodeMap namedNodeMap = map.getAttributes(); // Getting attributed of the nodes.
Node file_path = namedNodeMap.getNamedItem("full-path"); // getting the partcular node attribute
String path = file_path.getNodeValue(); // This will give the value of full-path
Parsing XML on Android
Your loop should look like this:
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
tv1.setText(tv1.getText()+"\nName : " + getValue("name", element)+"\n");
}
where getValue :
private static String getValue(String tag, Element element)
{
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = nodeList.item(0);
return node.getNodeValue();
}
How to parse xml in android-java?
You need to iterate ingredients
child nodes like you do it for recipe
tag.
But the more easy way is to use XPath.
How to parse xml string?
Well You Need to go to res > values > String.xml
you need to set your String in String.xml
In above image you need go to String xml
<resources>
<string name="app_name">DocumentRecord</string>
<string name="your_title">your title</string>
<string name="your_url"><a href="your_website"></a></string></resources>
How to fix Error Parsing XML in android development?
You have 2 problems.
First one : remove white space in textColor
attribute :
<TextView
android:id="@+id/number"
android:text="Number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:textColor="#373D4D"
android:background="#FFFFFF"/>
Second one is :
<Button
android:text="<<"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/curr"
android:layout_weight="1" />
You should extract the String <<
causing an issue at compilation. extract string resource like so :
<Button
android:text="@string/yourstring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/curr"
android:layout_weight="1" />
Then in strings.xml
(/res/values/strings.xml) add the created resource :
<string name="yourstring"><![CDATA[<<]]></string>
Related Topics
Multiple Dex Files Define Landroid/Support/Annotation/Animres
Using Asynctask with Passing a Value
Listview Selection Remains Persistent After Exiting Choice Mode
Tm.Getdeviceid() Is Deprecated
Open Source Face Recognition for Android
Android JSON Parsing of Multiple JSONobjects Inside JSONobject
What Is an Index in Elasticsearch
Calling a Java Servlet from JavaScript
Running a .SQL Script Using MySQL with Jdbc
Why Comparing Integer with Int Can Throw Nullpointerexception in Java
Open Gallery App from Android Intent
Driver Jdbc Postgresql with Android
Problems Importing Project into Android Studio Regarding Actionbarsherlock
Noclassdeffounderror on External Library Project for Android
Android - Get Value from Hashmap