如何在android中显示各种解析的XML文件?
我正在构建一个应用程序,其中我的模块之一包含天气预报。我正在使用 SAX 解析器,到目前为止,我正在解析 XML 文件并获取单个位置的结果,这里是:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saxmain);
ScrollView MyScrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
MyScrollView.addView(layout);
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://192.168.1.31:81/Udayavani/android/weather/weather.php?city=mumbai");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/* Get result from MyXMLHandler TagList Object */
taglist = MyXMLHandler.sitesList;
for (int i = 0; i <26; i++) {
texts[i] = new TextView(this);
texts[i].setText(""+i);
texts[i].setTypeface(kannada);
layout.addView(texts[i]);
}
/** Set the result text in textview and add it to layout */
texts[0].setText("Current= "+taglist.getCurrent().get(0));
texts[1].setText("Temp= "+taglist.getTemp().get(0));
texts[2].setText("Humidity= "+taglist.getHumidity().get(0));
texts[3].setText(""+taglist.getWind_condition().get(0));
texts[4].setText(""+taglist.getIcon().get(0));
int j=0;
int k=5;
while(k<20){
texts[k].setText("----------------------------------");
k++;
texts[k].setText("Day= "+taglist.getDay().get(j));
k++;
texts[k].setText("Condition= "+taglist.getCondition().get(j));
k++;
texts[k].setText("Low= "+taglist.getLow().get(j));
k++;
texts[k].setText("High= "+taglist.getHigh().get(j));
k++;
texts[k].setText("Icon= "+taglist.getIcon().get(j));
k++;
j++;
if(j>=5){
j=0;
}
}
/** Set the layout view to display */
setContentView(MyScrollView);
}
}
我的问题是那,有人可以帮我获取各个城市的已解析 Xml 文件吗?我有几乎所有城市的 URL,只需更改我在代码中提到的 URL 末尾的城市名称(例如,weather.php?city=auckland),我就可以得到它。我从ListView 和我将大约 20-30 个城市作为列表内容存储在 ListView 中。每当我单击特定位置时,我都想获取该特定城市的已解析 XMl 文件。
任何人请指导我。
谢谢
编辑:
xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<weather>
<current_conditions>
<current>ಹೊಗೆಯಾವೃತ</current>
<temp>95</temp>
<humidity>Humidity: ೨೩%</humidity>
<wind_condition>Wind: ಪೂ at 13 km/h</wind_condition>
<icon>smoke.gif</icon>
</current_conditions>
<forecast_information>
<forecast0>
<day>ಶನಿ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast0>
<forecast1>
<day>ರ.</day>
<condition>ಶುಭ್ರ</condition>
<low>25</low>
<high>35</high>
<icon>sunny.gif</icon>
</forecast1>
<forecast2>
<day>ಸೋ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast2>
<forecast3>
<day>ಮಂ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>26</low>
<high>33</high>
<icon>mostly_sunny.gif</icon>
</forecast3>
</forecast_information>
I am building an application in which one of my modules consists of weather forecasting.I am using SAX Parser and as of now,I am parsing the XML file and obtaining the result of a single place and here it goes:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saxmain);
ScrollView MyScrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
MyScrollView.addView(layout);
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://192.168.1.31:81/Udayavani/android/weather/weather.php?city=mumbai");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/* Get result from MyXMLHandler TagList Object */
taglist = MyXMLHandler.sitesList;
for (int i = 0; i <26; i++) {
texts[i] = new TextView(this);
texts[i].setText(""+i);
texts[i].setTypeface(kannada);
layout.addView(texts[i]);
}
/** Set the result text in textview and add it to layout */
texts[0].setText("Current= "+taglist.getCurrent().get(0));
texts[1].setText("Temp= "+taglist.getTemp().get(0));
texts[2].setText("Humidity= "+taglist.getHumidity().get(0));
texts[3].setText(""+taglist.getWind_condition().get(0));
texts[4].setText(""+taglist.getIcon().get(0));
int j=0;
int k=5;
while(k<20){
texts[k].setText("----------------------------------");
k++;
texts[k].setText("Day= "+taglist.getDay().get(j));
k++;
texts[k].setText("Condition= "+taglist.getCondition().get(j));
k++;
texts[k].setText("Low= "+taglist.getLow().get(j));
k++;
texts[k].setText("High= "+taglist.getHigh().get(j));
k++;
texts[k].setText("Icon= "+taglist.getIcon().get(j));
k++;
j++;
if(j>=5){
j=0;
}
}
/** Set the layout view to display */
setContentView(MyScrollView);
}
}
My question to SO is that,could anybody please help me to get the parsed Xml file of the various cities? I have the URL for almost all the cities and I can get that,just by changing the city name in the end of my URL(say weather.php?city=auckland) which I have mentioned in my code.I am starting with the ListView and I have around 20-30 cities stored in the ListView as list-contents.Whenever I click on the particular place I want to get the parsed XMl file of that particular city.
Anyone please guide me on this.
Thanks
EDITED:
xml contents:
<?xml version="1.0" encoding="UTF-8"?>
<weather>
<current_conditions>
<current>ಹೊಗೆಯಾವೃತ</current>
<temp>95</temp>
<humidity>Humidity: ೨೩%</humidity>
<wind_condition>Wind: ಪೂ at 13 km/h</wind_condition>
<icon>smoke.gif</icon>
</current_conditions>
<forecast_information>
<forecast0>
<day>ಶನಿ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast0>
<forecast1>
<day>ರ.</day>
<condition>ಶುಭ್ರ</condition>
<low>25</low>
<high>35</high>
<icon>sunny.gif</icon>
</forecast1>
<forecast2>
<day>ಸೋ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>25</low>
<high>36</high>
<icon>mostly_sunny.gif</icon>
</forecast2>
<forecast3>
<day>ಮಂ.</day>
<condition>ಬಹುಪಾಲು ಬಿಸಿಲು</condition>
<low>26</low>
<high>33</high>
<icon>mostly_sunny.gif</icon>
</forecast3>
</forecast_information>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了解决方案。我声明了一个名为 StateNames[] 的位置数组。并且从我的 ListView 类中,我将 putExtra("id",pos) 传递给我声明数组的类,并且我通过下列的:
I got the solution.I am declaring an array of places called StateNames[].And from my ListView class I am passing putExtra("id",pos) to the class where I have declared my array,and I am accessing it through the following:
使用列表视图而不是滚动视图。使用列表视图的 onListItemClick() 方法,获取单击的特定列表项(在你的情况下为城市)并将其作为参数传递给你的函数(它应该能够获取你传递给 url 的城市的 xml 文件)。有关列表视图的详细信息请参阅此
Use List view instead of scroll view. Using onListItemClick() method of list view, get the particular list item(city in ur case) clicked and pass it as a parameter to ur function(which should be able to get xml file of the city u passed to the url). For more info on list view see this