I want to read xml file which looks like the following ... I have stored it in assets folder with
我想要读取如下所示的xml文件……我已经把它存储在assets文件夹中了。
<ImageList SpriteSheetName="hh_gmw01">
<Image Name="gmw01" x="0" y="0" width="1047" height="752"/>
<Image Name="hht1l01" x="388" y="269" width="34" height="36"/>
<Image Name="hht1l02" x="147" y="99" width="85" height="33"/>
</ImageList>
How do I get these values?
我如何得到这些值?
5 个解决方案
#1
5
Try this out first:
试试这个:
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
Button btn;
TextView tvXml;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Load XML for parsing.
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("textxml.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
String s = readTextFile(inputStream);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(s);
}
});
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
}
#2
2
There is several ways to read a XML in Android. My first option is DocumentBuilder since do not create an API version restriction (is available since API Level 1).
在Android中有几种读取XML的方法。我的第一个选项是DocumentBuilder,因为不创建API版本限制(从API级别1开始就可用)。
An example from one of my projects:
我的一个项目的一个例子:
public Document parseXML(InputSource source) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(source);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
How do you read the file and access the document values after this, well, its pretty basic, just Google it.
如何读取文件并在此之后访问文档值,它非常基本,就是谷歌。
#3
2
To start with use DOM parser. Since its much lesser code, and easy to follow. SAX parser is just too much code to start with. People will argue that SAX is faster, yes it is, but DOM is easier, lesser code and lesser bugs.
首先使用DOM解析器。因为它的代码少得多,而且容易遵循。SAX解析器从一开始就是太多的代码。人们会认为SAX更快,是的,但是DOM更简单,代码更少,bug更少。
If you must move to SAX, first measure your response times when using DOM, and only if parsing is causing you the most pain, then move to SAX. Or else DOM does a wonderful job.
如果您必须移动到SAX,那么首先在使用DOM时度量您的响应时间,并且只有在解析使您感到最痛苦的情况下,然后移动到SAX。或者DOM做得很好。
#4
1
Personally i wouldn't recommend the DOM parser, try this instead, simple annotations can help you by using the Simple xml parser
我个人并不推荐DOM解析器,而是尝试一下,简单的注释可以通过使用简单的xml解析器来帮助您
http://www.javacodegeeks.com/2011/02/android-xml-binding-simple-tutorial.html
http://www.javacodegeeks.com/2011/02/android-xml-binding-simple-tutorial.html
#5
-1
Try this for Xamarin.Android (Cross Platform)
Xamarin试试这个。Android(跨平台)
you need to store xml file into Assets folder with build action "AndroidAsset".
您需要使用“AndroidAsset”构建操作将xml文件存储到Assets文件夹中。
using System;
using System.Xml;
using System.IO;
Code snippet to read xml file
读取xml文件的代码片段
XmlDocument xDoc = new XmlDocument();
Stream xmlStream = Assets.Open("textxml.xml");
xDoc.Load(xmlStream);
XmlDocument class implements the core XML Document Object Model (DOM) parser for the .NET Framework.
XmlDocument类为. net框架实现核心XML文档对象模型(DOM)解析器。
#1
5
Try this out first:
试试这个:
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
Button btn;
TextView tvXml;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Load XML for parsing.
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("textxml.xml");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
String s = readTextFile(inputStream);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(s);
}
});
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
}
#2
2
There is several ways to read a XML in Android. My first option is DocumentBuilder since do not create an API version restriction (is available since API Level 1).
在Android中有几种读取XML的方法。我的第一个选项是DocumentBuilder,因为不创建API版本限制(从API级别1开始就可用)。
An example from one of my projects:
我的一个项目的一个例子:
public Document parseXML(InputSource source) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(source);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
How do you read the file and access the document values after this, well, its pretty basic, just Google it.
如何读取文件并在此之后访问文档值,它非常基本,就是谷歌。
#3
2
To start with use DOM parser. Since its much lesser code, and easy to follow. SAX parser is just too much code to start with. People will argue that SAX is faster, yes it is, but DOM is easier, lesser code and lesser bugs.
首先使用DOM解析器。因为它的代码少得多,而且容易遵循。SAX解析器从一开始就是太多的代码。人们会认为SAX更快,是的,但是DOM更简单,代码更少,bug更少。
If you must move to SAX, first measure your response times when using DOM, and only if parsing is causing you the most pain, then move to SAX. Or else DOM does a wonderful job.
如果您必须移动到SAX,那么首先在使用DOM时度量您的响应时间,并且只有在解析使您感到最痛苦的情况下,然后移动到SAX。或者DOM做得很好。
#4
1
Personally i wouldn't recommend the DOM parser, try this instead, simple annotations can help you by using the Simple xml parser
我个人并不推荐DOM解析器,而是尝试一下,简单的注释可以通过使用简单的xml解析器来帮助您
http://www.javacodegeeks.com/2011/02/android-xml-binding-simple-tutorial.html
http://www.javacodegeeks.com/2011/02/android-xml-binding-simple-tutorial.html
#5
-1
Try this for Xamarin.Android (Cross Platform)
Xamarin试试这个。Android(跨平台)
you need to store xml file into Assets folder with build action "AndroidAsset".
您需要使用“AndroidAsset”构建操作将xml文件存储到Assets文件夹中。
using System;
using System.Xml;
using System.IO;
Code snippet to read xml file
读取xml文件的代码片段
XmlDocument xDoc = new XmlDocument();
Stream xmlStream = Assets.Open("textxml.xml");
xDoc.Load(xmlStream);
XmlDocument class implements the core XML Document Object Model (DOM) parser for the .NET Framework.
XmlDocument类为. net框架实现核心XML文档对象模型(DOM)解析器。