问题
I am not experienced in xml parsing so maybe some of the things I write look stupid to some and maybe some of my terminology is not quite correct.. Please forgive.
I develop an android app, which needs among others to parse weather data from YR.no. This organization offers an api with methods that provide certain data on xml format. Let’s say for example I want to parse xml data from this http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad
I developed a code that can do some xml parsing and it works right in this http://www.w3schools.com/xml/simple.xml (as a test).
The main code lines to define what to get in my BaseFeedParser class are:
RootElement root2 = new RootElement("breakfast_menu");
Element food = root2.getChild("food");
Element name = food.getChild("name");
food.setEndElementListener(new EndElementListener() {
public void end() {
messages.add(currentMessage.copy());
}
});
food.getChild("name").setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentMessage.setTitle(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root2.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
And then from my activity class:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
loadFeed();
}
private void loadFeed() {
try {
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
System.out.println(messages.size());
for (Message msg : messages) {
titles.add(msg.getTitle());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
String str = "!";
if (titles != null) {
str = titles.toString();
System.out.println("not null");
System.out.println(str);
}
test(str);
} catch (Throwable t) {
Log.e("AndroidNews",t.getMessage(), t);
}
}
public void test(String s) {
setContentView(R.layout.error);
TextView textView = (TextView) findViewById(R.id.mytextview);
textView.setText(s);
}
So it returns and prints the data I want (“Belgian Waffles” etc)
My problem with the yr.no data that I originally wanted to parse is that every end child does not contain just one value but can have more tags (e.g. <waveDirection unit="degree" value="250"/>
). So when I change the elements to use this one, it ends to 25 different String
s (which if you count are all the different children with tag waveDirection
) but every value is empty (like a String a = ""
). I get no error, I just get a list of 25 empty strings. The way I try to reach my element is something like:
RootElement root = new RootElement("weatherdata");
Element product = root.getChild("product");
Element time = product.getChild("time");
Element location = time.getChild("location");
location .setEndElementListener(new EndElementListener(){
public void end() {
messages.add(currentMessage.copy());
}
});
location.getChild("windDirection").setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentMessage.setTitle(body);
}
});
So how should I modify this so that it works with this xml? I do not provide all the classes and methods (like setTitle()
) but I think they work since they parse right my first test xml. And I suppose I set my feedUrlString = "http://api.yr.no/weatherapi/seaapproachforecast/1.0/?location=stad";
correctly since it finds the root of the document and 25 elements.
EDIT: I did it! The right way to get the attributes was to use:
location.setStartElementListener(new StartElementListener(){
public void start(Attributes attributes){
messages.add(currentMessage.copy());
}
});
location.getChild("windDirection").setTextElementListener(new TextElementListener(){
public void end(String body) {
//currentMessage.setTitle(body);
//System.out.println("b="+ body);
}
@Override
public void start(Attributes attributes) {
System.out.println("val" + attributes.getValue("deg"));
currentMessage.setTitle(attributes.getValue("deg"));
}
});
So now I get my data but for some reason all except the very last element (I tested it for other YR.no xmls as well).. There must be some bug that I should solve but the major step is done. Thank you all for the answers and especially user306848 who pointed me to the direction I used!
回答1:
I think your code assumes there is text node which represents the values you seek. Which is not the case for the xml file from the yr.no domain.
You need to figure out how to read attributes from tags for the xml library you use.
I have no experience with android development, but do you use the android.sax package? Then i think android.sax.StartElementListener would be a good candidate. It receives the attributes that belong to a specific tag.
回答2:
Use Dom Parser.......It will be easy...
See some tutorial from here,
http://www.roseindia.net/xml/dom/
回答3:
Use this example.
He is using a XML file stored locally. If your getting a XML Feed change the code to the following:
URL url = new URL("ENTER XML LINK");
InputStream stream = url.openStream();
Document doc = docBuilder.parse(stream);
来源:https://stackoverflow.com/questions/9499929/parse-xml-from-internet-yr-no