パーサーにxmlファイルの1つのセクションだけを読み取らせて表示させるには、誰かの助けが本当に必要です。上から下に2つ読み取ることができますが、xmlハンドラーで各セクションを区切る内部タグを使用しようとするとそれはそれを行わず、精神的になり、それの1ビットを示します
これは私のパーサーのコードです
package heavytime
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
//import android.content.Intent;
import android.os.Bundle;
//import android.view.View;
//import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Mainweather extends Seskanoreweatherpart2Activity {
weatherlist sitesList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
layout.setBackgroundColor(0xFF000080);
TextView value [];
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.seskanore.com/currentoutput.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
XMLhandler XMLhandler = new XMLhandler();
xr.setContentHandler(XMLhandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from XMLhandler SitlesList Object */
sitesList = XMLhandler.sitesList;
/** Assign textview array lenght by arraylist size */
// item = new TextView[sitesList.getName().size()];
value = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
value[i] = new TextView(this);
value[i].setText(sitesList.getvalue().get(i)+ " is " +sitesList.getitem().get(i));
layout.addView(value[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
これは私のXMLハンドラーのコードです
package heavytime
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLhandler extends DefaultHandler {
Boolean currentElement = false;
Boolean temperature = false;
String currentValue = null;
// int currentVal = 0;
public static weatherlist sitesList = null;
public static weatherlist getSitesList() {
return sitesList;
}
public static void setSitesList(weatherlist sitesList) {
XMLhandler.sitesList = sitesList;
}
@Override
public void startDocument() throws SAXException {
// Some sort of setting up work
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("weatherdata"))
{
sitesList = new weatherlist();}
else if (localName.equals("item")){
temperature = true;
String attr = attributes.getValue("name");
sitesList.setValue(attr);}
}
/** Called when tag closing*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value*/
if (localName.equals("temperaturetags")){
localName.equalsIgnoreCase("item");
sitesList.setName(currentValue);
localName.equalsIgnoreCase("value");
sitesList.setitem(currentValue);
}
temperature = false;
}
/** Called to get tag characters*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
@Override
public void endDocument() throws SAXException {
// Some sort of finishing up work
}
}
これは、解析された情報を表示するための私の配列リストのコードです
package heavytime
import java.util.ArrayList;
public class weatherlist {
private ArrayList<String> value = new ArrayList<String>();
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> item = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getvalue() {
return value;
}
public void setValue(String value) {
this.value.add(value);
}
public ArrayList<String> getName() {
return name;
}
public void setName(String name) {
this.name.add(name);
}
public ArrayList<String> getitem() {
return item;
}
public void setitem(String item) {
this.item.add(item);
}
}
温度タグ内のタグだけを表示したいのですが、iveはすべてを試しましたが、何も機能しないようです誰かが私を助けてくれませんか
とても有難い