gpt4 book ai didi

java - Android SAXParser,解析成数组并获取子节点

转载 作者:行者123 更新时间:2023-12-01 14:30:50 28 4
gpt4 key购买 nike

我正在开发一个小型应用程序,该应用程序使用 xml 文件来打印章节的 ArrayList,后者又指向特定的 html 文件。

我使用本教程开始:http://www.anddev.org/novice-tutorials-f8/parsing-xml-from-the-net-using-the-saxparser-t353.html

我的 xml 文件看起来像这样:

<chapters>
<chapter title="Förutsättningar">
<page title="Sida 3" url="sida_3.html" />
<page title="Sida 4" url="sida_4.html" />
</chapter>
</chapters>

使用上面的教程,我已经设法将每个章节节点输出到一个 ArrayList 中,每个项目都有一个 onListItemClick 函数。到目前为止一切顺利。

我遇到的问题是,当我单击某个项目时,我无法弄清楚如何获取特定的子节点并加载 html 文件。我是 Android 的新手。

有什么想法吗?如果能就此问题提供任何帮助,我将不胜感激。

这是我的来源:

解析XML.java

public class ParsingXML extends ListActivity {
private final String MY_DEBUG_TAG = "XmlParser";
public String lang = null;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("Lastsäkring");

Bundle bundle = this.getIntent().getExtras();
lang = bundle.getString("lang");

Log.i("ParsingXML", "Chosen language: " + this.lang + ", Type: " + this.lang.getClass().getName());

TextView tv = new TextView(this);

try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);

/*
* If XML-file is located online (needs internet permissions in the manifest):
* URL url = new URL("http://dev.houdini.se/android/demo.xml");
* xr.parse(new InputSource(url.openStream()));
*/

if(this.lang.equals("en"))
xr.parse(new InputSource(this.getResources().openRawResource(R.raw.en_content)));
else
xr.parse(new InputSource(this.getResources().openRawResource(R.raw.sv_content)));

ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, parsedExampleDataSet.toArrayList()));

} catch(Exception e) {
tv.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "XmlParseError", e);
this.setContentView(tv);
}
}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;

CharSequence text = "Clicked position: " + position + ", id: " + id;
Toast toast = Toast.makeText(context, text, duration);
toast.show();


/*switch( position )
{
case 0:
Bundle bundle = new Bundle();
bundle.putString("WindowTitle", "TESTA");
Intent intent = new Intent(this, TextPage.class);
intent.putExtras(bundle);
startActivity(intent);
break;
case 1:
Intent video = new Intent(this, Video.class);
startActivity(video);
break;

case 2:
Intent swipe = new Intent(this, Swipe.class);
startActivity(swipe);
break;
}*/
}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent prefsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(prefsActivity);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

ExampleHandler.java

public class ExampleHandler extends DefaultHandler {
private boolean in_chapters = false;
private boolean in_chapter = false;
private boolean in_page = false;

private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}

@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}

@Override
public void endDocument() throws SAXException {
// Nothing to do
}

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equals("chapters")) {
this.in_chapters = true;
} else if(localName.equals("chapter")) {
this.in_chapter = true;
String attrValue = atts.getValue("title");
myParsedExampleDataSet.setExtractedString(attrValue);
} else if(localName.equals("page")) {
this.in_page = true;
}
}

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if(localName.equals("chapters")) {
this.in_chapters = false;
} else if(localName.equals("chapter")) {
this.in_chapter = false;
} else if(localName.equals("page")) {
this.in_page = false;
}
}

@Override
public void characters(char ch[], int start, int length) {
if(this.in_page == true) {
myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
}
}

}

ParsedExampleDataSet.java

public class ParsedExampleDataSet {
private String extractedString = "";
private ArrayList<String> myArr = new ArrayList<String>();
private int extractedInt = 0;

public ArrayList<String> getExtractedString() {
//return extractedString; Function Type = String
return myArr;
}

public void setExtractedString(String extractedString) {
//this.extractedString += extractedString + "\n";
myArr.add(extractedString);
}

public int getExtractedInt() {
return extractedInt;
}

public void setExtractedInt(int extractedInt) {
this.extractedInt = extractedInt;
}

public String toString() {
return "NODER\n" + this.extractedString;
}

public ArrayList<String> toArrayList() {
return this.myArr;
}
}

最佳答案

首先创建合适的数据结构:

public class PageNode {
public String title;
public String url;
/* Getters/setter/constructor etc. if you feel like*/
public String toString() {
return title;
}
}

public class ChapterNode {
public String title;
public ArrayList<PageNode> pages = new ArrayList<PageNode>();
/* Getters/setter/constructor etc. if you feel like*/
}

并相应地解析 xml。示例:

ArrayList<ChapterNode> chapters = new ArrayList<ChapterNode>();
ChapterNode chapterNode = null;

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equals("chapters")) {
} else if(localName.equals("chapter")) {
chapterNode = new ChapterNode();
chapterNode.title = atts.getValue("title");
} else if(localName.equals("page")) {
PageNode pageNode = new PageNode();
pageNode.title = atts.getValue("title");
pageNode.url = atts.getValue("url");
chapterNode.pages.add(pageNode);
}
}

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if(localName.equals("chapters")) {
} else if(localName.equals("chapter")) {
chapters.add(chapterNode);
chapterNode = null;
} else if(localName.equals("page")) {
}
}

然后你可以像这样访问pageNode:

PageNode pageNode = chapterNode.pages.get(position);

然后像这样设置适配器:

this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, chapterNode.pages));

关于java - Android SAXParser,解析成数组并获取子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8354598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com