无法 import/convert 采样 XML 文件到字符串进行解析
Cannot import/convert sample XML file to string for parsing
我的问题更像是 "what am I doing wrong here?" 我正在尝试导入我从 W3.schools 获得的示例 XML 文档。我尝试 DOM 获取可解析文件,BufferStream(new FileBuilder(new File(xxx)) 将其转换为字符串,然后偶然发现 XPullParser 试图将文档解析为工作元素。我我已经用 maven 和 DOM 完成了这个,但是 Android 似乎有点挑剔。我查看了整个 Whosebug 和其他文档,但我似乎找不到答案。
这是我得到的:
在MainActivity中初始化对象
public void OnClickDisplayResults(View view) {
final Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText(searchResults.initiateParsing(), TextView.BufferType.EDITABLE);
}
});
}
工厂
public String initiateParsing() {
String result = "";
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("plant_catalog.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return libInterface.getXmlResult();
}
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<SearchResults> products = null;
int eventType = parser.getEventType();
SearchResults currentProduct = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
products = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("PLANT")){
currentProduct = new SearchResults();
} else if (currentProduct != null){
if (name.equals("COMMON")){
currentProduct.name = parser.nextText();
} else if (name.equals("BOTANICAL")){
currentProduct.color = parser.nextText();
} else if (name.equals("PRICE")){
currentProduct.quantity= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
try {
if (name.equalsIgnoreCase("PLANT") && currentProduct != null) {
products.add(currentProduct);
}
}catch (Exception e) {
e.printStackTrace();
}
}
eventType = parser.next();
}
printProducts(products);
}
private void printProducts(ArrayList<SearchResults> products)
{
String content = "";
Iterator<SearchResults> it = products.iterator();
while(it.hasNext())
{
SearchResults currProduct = it.next();
content = content + "nnnProduct :" + currProduct.name + "n";
content = content + "Quantity :" + currProduct.quantity + "n";
content = content + "Color :" + currProduct.color + "n";
}
libInterface.setXmlResult(content);
}
堆栈跟踪
Process: com.example.myapp, PID: 7441
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
at com.app.StateLibraryApplication.SearchResults.initiateParsing(SearchResults.java:48)
at com.app.StateLibraryApplication.MyActivity.onClick(MyActivity.java:54)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5256)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
02-22 13:00:24.527 7441-7441/com.example.myapp I/Process﹕ Sending signal. PID: 7441 SIG: 9
我就是这样解决自己的问题的
我读到了 Context。
上下文是引发大多数错误的原因。
您必须将方法更改为:
public String initiateParsing(Context context) {
String result = "";
FileInputStream fis = null;
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
// uses Context to getApplicationContext().getResources();
Resources res = context.getApplicationContext().getResources();
// Use direct Resource Tag with R
InputStream in_st = res.openRawResource(R.raw.plant_catalog);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_st, null);
parseXML(parser); // and so forth.....
主要是方法:thisIsMyMethod(Context context) { context.SOMETHING }
希望这可以为其他人节省 6 小时的研究时间。
我的问题更像是 "what am I doing wrong here?" 我正在尝试导入我从 W3.schools 获得的示例 XML 文档。我尝试 DOM 获取可解析文件,BufferStream(new FileBuilder(new File(xxx)) 将其转换为字符串,然后偶然发现 XPullParser 试图将文档解析为工作元素。我我已经用 maven 和 DOM 完成了这个,但是 Android 似乎有点挑剔。我查看了整个 Whosebug 和其他文档,但我似乎找不到答案。
这是我得到的:
在MainActivity中初始化对象
public void OnClickDisplayResults(View view) {
final Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText(searchResults.initiateParsing(), TextView.BufferType.EDITABLE);
}
});
}
工厂
public String initiateParsing() {
String result = "";
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("plant_catalog.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return libInterface.getXmlResult();
}
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
ArrayList<SearchResults> products = null;
int eventType = parser.getEventType();
SearchResults currentProduct = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
products = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equals("PLANT")){
currentProduct = new SearchResults();
} else if (currentProduct != null){
if (name.equals("COMMON")){
currentProduct.name = parser.nextText();
} else if (name.equals("BOTANICAL")){
currentProduct.color = parser.nextText();
} else if (name.equals("PRICE")){
currentProduct.quantity= parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
try {
if (name.equalsIgnoreCase("PLANT") && currentProduct != null) {
products.add(currentProduct);
}
}catch (Exception e) {
e.printStackTrace();
}
}
eventType = parser.next();
}
printProducts(products);
}
private void printProducts(ArrayList<SearchResults> products)
{
String content = "";
Iterator<SearchResults> it = products.iterator();
while(it.hasNext())
{
SearchResults currProduct = it.next();
content = content + "nnnProduct :" + currProduct.name + "n";
content = content + "Quantity :" + currProduct.quantity + "n";
content = content + "Color :" + currProduct.color + "n";
}
libInterface.setXmlResult(content);
}
堆栈跟踪
Process: com.example.myapp, PID: 7441
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
at com.app.StateLibraryApplication.SearchResults.initiateParsing(SearchResults.java:48)
at com.app.StateLibraryApplication.MyActivity.onClick(MyActivity.java:54)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5256)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
02-22 13:00:24.527 7441-7441/com.example.myapp I/Process﹕ Sending signal. PID: 7441 SIG: 9
我就是这样解决自己的问题的
我读到了 Context。
上下文是引发大多数错误的原因。
您必须将方法更改为:
public String initiateParsing(Context context) {
String result = "";
FileInputStream fis = null;
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
// uses Context to getApplicationContext().getResources();
Resources res = context.getApplicationContext().getResources();
// Use direct Resource Tag with R
InputStream in_st = res.openRawResource(R.raw.plant_catalog);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_st, null);
parseXML(parser); // and so forth.....
主要是方法:thisIsMyMethod(Context context) { context.SOMETHING }
希望这可以为其他人节省 6 小时的研究时间。