gpt4 book ai didi

java - Android Xml 处理程序仅保留我的 xml 文件中的最后一个对象

转载 作者:行者123 更新时间:2023-12-01 15:47:37 26 4
gpt4 key购买 nike

我有一个名为 ExampleHandler 扩展自 DefaultHandler 的类。

此类从外部 xml 文件(在网络上)读取数据,该 xml 文件的结构为:

<resultsSet>
<result>
<title>WinRANI Web Services!</title>
<nom>DADI</nom>
<prenom>Morad</prenom>
<adresse>DANS MES REVES</adresse>
</result>
...
</resultsSet>

我创建了一个与此 xml 文件具有相同结构的类,名为 ParsedExampleDataSet(它具有 getter 和 setter)。

我还创建了一个ArrayList,它将包含所有“结果”对象,但问题是当处理程序读取所有对象时,ArrayList中的所有项目> 是一样的。

这是我的代码:

package com.example.helloandroid;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class ExampleHandler extends DefaultHandler{

// ===========================================================
// Fields
// ===========================================================

private boolean in_resultset = false;
private boolean in_result = false;
private boolean in_title = false;
private boolean in_nom = false;
private boolean in_prenom = false;
private boolean in_adresse = false;
private boolean in_tel = false;
private boolean in_fax = false;
private boolean in_lon = false;
private boolean in_lat = false;
private boolean in_description = false;
private boolean in_infos = false;

private test t;
private int currentIndex = 0;
ParsedExampleDataSet[] p = new ParsedExampleDataSet[5];

private ArrayList<ParsedExampleDataSet> myParsedExampleDataSetList = new ArrayList<ParsedExampleDataSet>();
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

private ParsedExampleDataSet s1;
private ParsedExampleDataSet s2;
private ParsedExampleDataSet s3;
private ParsedExampleDataSet s4;
private ParsedExampleDataSet s5;


// ===========================================================
// Getter & Setter
// ===========================================================

public ArrayList<ParsedExampleDataSet> getParsedData() {
return this.myParsedExampleDataSetList;
}

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

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

/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("resultset")) {
this.in_resultset = true;
}else if (localName.equals("result")) {
this.in_result = true;
}else if (localName.equals("title")) {
this.in_title = true;
}else if (localName.equals("nom")) {
// Extract an Attribute
//String attrValue = atts.getValue("thenumber");
//int i = Integer.parseInt(attrValue);
//myParsedExampleDataSet.setExtractedInt(i);
this.in_nom = true;
}else if (localName.equals("prenom")) {
this.in_prenom = true;
}else if (localName.equals("tel")) {
this.in_tel = true;
}else if (localName.equals("fax")) {
this.in_fax = true;
}else if (localName.equals("lon")) {
this.in_lon = true;
}else if (localName.equals("lat")) {
this.in_lat = true;
}else if (localName.equals("description")) {
this.in_description = true;
}else if (localName.equals("infos")) {
this.in_infos = true;
}
}

/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("resultset")) {
this.in_resultset = false;

}else if (localName.equals("result")) {

this.in_result = false;
myParsedExampleDataSetList.add(myParsedExampleDataSet);

}else if (localName.equals("title")) {
this.in_title = false;
}else if (localName.equals("nom")) {
// Nothing to do here
this.in_nom = false;
}else if (localName.equals("prenom")) {
this.in_prenom = false;
}else if (localName.equals("tel")) {
this.in_tel = false;
}else if (localName.equals("fax")) {
this.in_fax = false;
}else if (localName.equals("lon")) {
this.in_lon = false;
}else if (localName.equals("lat")) {
this.in_lat = false;
}else if (localName.equals("description")) {
this.in_description = false;
}else if (localName.equals("infos")) {
this.in_infos = false;
}
}

/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_title){
myParsedExampleDataSet.setExtractedTitle(new String(ch, start, length));
}else if (this.in_nom){
myParsedExampleDataSet.setExtractedNom(new String(ch,start, length));
}else if (this.in_prenom){
myParsedExampleDataSet.setExtractedPrenom(new String(ch,start, length));
}else if (this.in_tel){
myParsedExampleDataSet.setExtractedTel(new String(ch,start, length));
}else if (this.in_fax){
myParsedExampleDataSet.setExtractedFax(new String(ch,start, length));
}else if (this.in_lon){
myParsedExampleDataSet.setExtractedLon(new String(ch,start, length));
}else if (this.in_lat){
myParsedExampleDataSet.setExtractedLat(new String(ch,start, length));
}else if (this.in_description){
myParsedExampleDataSet.setExtractedDescription(new String(ch,start, length));
}else if (this.in_infos){
myParsedExampleDataSet.setExtractedInfos(new String(ch,start, length));
}

}
}

我的 xml 文件还有一些其他字段。

最佳答案

这是对 Java 的一个常见误解。你必须记住,java 将所有非基元视为指针。在您的情况下,您只需新建一次数据容器对象。因此,当您调用 setter 和 getter 时,您总是会影响多次添加到数组中的同一个对象。

每次向其中添加一组新条目时,您都可以通过新建一个新容器 (ParsedExampleDataSet) 来解决此问题。

else if (localName.equals("结果")) {

   this.in_result = true;
myParsedExampleDataSet = new ParsedExampleDataSet();

}

还请记住,SAXParser 可以根据字段中的内容对每个字段多次调用characters(..)。因此,您可能希望将从字符获得的值连接到该字段已有的值。

如果(this.in_title){

String value = myParsedExampleDataSet.getExtractedTitle();
value += new String(ch, start, length)
myParsedExampleDataSet.setExtractedTitle(value);

}

关于java - Android Xml 处理程序仅保留我的 xml 文件中的最后一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6838095/

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