Retrieve data from xml with XmlSax

susu

Well-Known Member
Licensed User
Longtime User
I got xml file like this

B4X:
<year>1521</year> 
<content>Martin Luther is excommunicated from the Catholic Church.</content> 
<year>1777</year> 
<content>General George Washington defeats the British led by British General Lord Charles Cornwallis, at Princeton, New Jersey.</content> 
<year>1861</year> 
<content>Delaware rejects a proposal that it join the South in seceding from the Union.</content> 
<year>1903</year> 
<content>The Bulgarian government renounces the Treaty of Commerce tying it to the Austro-Hungarian empire.</content>

How can I retrieve only the content of year 1861 (Delaware rejects a proposal that it join the South in seceding from the Union) ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim parser As SaxParser
    Dim year As String
    Dim content As String
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If name = "year" Then
        year = Text
    Else If year = "1861" AND name = "content" Then
        content = Text
    End If
End Sub

The required value will be stored in 'content' after parsing.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thank you Erel. How about its speed, compare to CSV database?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thank you again.
 
Upvote 0

brindha

New Member
xml SAX parser can not retrive images in xml

Hello,

Can not xml SAX parser retrieve images from xml.
This is the xml code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>itemChildren</key>
<array>

<dict>

<title>Roasted Garlic Jam</title>
<img>image1.png</img>
<desc>
Ingredients:
4 heads garlic, large roasted
1 and peeled
2 teaspoon olive oil
1 tablespoon fresh lemon juice
1/2 teaspoon kosher salt
1 tablespoon Italian parsley, coarsely
1 chopped
1 cayenne pepper, pinch
1 ground pepper, fresh

1. Chop garlic with knife until it forms a paste. 2. Place in a bowl, add
remaining ingredients. Stir to blend. 3. Use for bruschetta, pizza or grilled meats. Yield 2/3 cup.
</desc>
</dict>
</array>
</dict>
</plist>

This is code

package com.EaseApps.jamrecipes;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

import android.content.Context;
import android.util.Log;

public class xmlAnalyzer extends DefaultHandler {

// Used to define what elements we are currently in
private boolean inImg = false;
private boolean inTitle = false;
private boolean inDesc = false;

// Feed and Article objects to use for temporary storage
private List<String> detailsItem = null;
private List<String> menuItem = null;
private List<String> ImgItem = null;
private String currentMenu = "", currentDetail = "", currentImg = "";


public void startElement(String uri, String name, String qName,
Attributes atts) {
if (name.trim().equals("title"))
inTitle = true;
else if (name.trim().equals("img"))
inImg = true;
else if (name.trim().equals("desc"))
inDesc = true;
}

public void endElement(String uri, String name, String qName)
throws SAXException {
if (name.trim().equals("title")) {
inTitle = false;
menuItem.add(currentMenu);
currentMenu = null;
} else if (name.trim().equals("img")) {
inImg = false;
ImgItem.add(currentImg);
currentImg = null;
} else if (name.trim().equals("desc")) {
inDesc = false;
detailsItem.add(currentDetail);
currentDetail = null;
}
}

public void characters(char ch[], int start, int length) {
String chars = (new String(ch).substring(start, start + length));
try {
if (inTitle) {
currentMenu = chars;
} else if (inImg) {
currentImg = chars;
} else if (inDesc) {
currentDetail = currentDetail + chars;
}
} catch (Exception e) {
Log.e("NewsDroid", e.toString());
e.printStackTrace();
}
}

public xmlAnalyzer(Context ctx, List<String> detail, List<String> menu,
List<String> img) {
detailsItem = detail;
menuItem = menu;
ImgItem = img;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(this);
xr.parse(new InputSource(ctx.getAssets().open("recipe.xml")));

} catch (IOException e) {
Log.e("Recipe", e.toString());
e.printStackTrace();
} catch (SAXException e) {
Log.e("Recipe", e.toString());
e.printStackTrace();
} catch (ParserConfigurationException e) {
Log.e("Recipe", e.toString());
e.printStackTrace();
}
}

@Override
public void endDocument() throws SAXException {

}

}

anyone pls help me it's urgent.

Thanks.
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Hi Erel

Hi Erel, i hope you can help me a little bit

I want to use the Wikipedia api to get the wiki text into my applikation.
For example, i enter into a edittext word "google" and then a label shows the wiki text --> Google Inc. is an American multinational Internet and software corporation specialized in Internet search, cloud computing, and advertising technologies.

I have this xml code -->

B4X:
<SearchSuggestion version="2.0">
<Query xml:space="preserve">india</Query>
−
<Section>
−
<Item>
<Text xml:space="preserve">India</Text>
−
<Description xml:space="preserve">
India (), officially the Republic of India (, '), is a country in South Asia. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/India</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Indischer_Maler_des_6._Jahrhunderts_001.jpg/48px-Indischer_Maler_des_6._Jahrhunderts_001.jpg" width="48" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indiana</Text>
−
<Description xml:space="preserve">
Indiana () is a US state, admitted to the United States as the 19th on December 11, 1816. It is located in the midwestern and Great Lakes regions of North America. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indiana</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/d/d2/Natives_guiding_french_explorers_through_indiana.jpg/50px-Natives_guiding_french_explorers_through_indiana.jpg" width="50" height="31"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indianapolis</Text>
−
<Description xml:space="preserve">
Indianapolis  (abbreviated Indy ) is the capital of the U.S. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indianapolis</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/IndianapolisC12.png/34px-IndianapolisC12.png" width="34" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian National Congress</Text>
−
<Description xml:space="preserve">
The Indian National Congress () (abbreviated INC, and commonly known as the Congress) is one of the two major political parties in India, the other being the Bharatiya Janata Party (BJP). 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/Indian_National_Congress
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Jawaharlal_Nehru.jpg/48px-Jawaharlal_Nehru.jpg" width="48" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian Ocean</Text>
−
<Description xml:space="preserve">
The Indian Ocean is the third largest of the world's oceanic divisions, covering approximately 20% of the water on the Earth's surface. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indian_Ocean</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Indian_Ocean-CIA_WFB_Map.png/50px-Indian_Ocean-CIA_WFB_Map.png" width="50" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indianapolis Colts</Text>
−
<Description xml:space="preserve">
The Indianapolis Colts are a professional American football team based in Indianapolis. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indianapolis_Colts</Url>
</Item>
−
<Item>
<Text xml:space="preserve">Indiana University</Text>
−
<Description xml:space="preserve">
Indiana University is a multi-campus public university system in the state of Indiana, United States. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indiana_University</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/5/5d/Indiana_University_seal.svg/50px-Indiana_University_seal.svg.png" width="50" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian subcontinent</Text>
−
<Description xml:space="preserve">
The Indian subcontinent, sometimes called the Indo-Pak subcontinent in Pakistan, or simply the Subcontinent, is a region of the Asia situated mostly on the Indian tectonic plate.
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indian_subcontinent</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Indian_subcontinent.JPG/40px-Indian_subcontinent.JPG" width="40" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian Rebellion of 1857</Text>
−
<Description xml:space="preserve">
The Indian Rebellion of 1857 began as a mutiny of sepoys of the British East India Company's army on 10 May 1857, in the town of Meerut, and soon escalated into other mutinies and civilian rebellions largely in the upper Gangetic plain and central India, with the major hostilities confined to present-day Uttar Pradesh, Bihar, northern Madhya Pradesh, and the Delhi region. 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/Indian_Rebellion_of_1857
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Indian_Rebellion_of_1857.jpg/50px-Indian_Rebellion_of_1857.jpg" width="50" height="42"/>
</Item>
−
<Item>
<Text xml:space="preserve">India national cricket team</Text>
−
<Description xml:space="preserve">
The Indian cricket team is the national cricket team of India. 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/India_national_cricket_team
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/8/8d/Cricket_India_Crest.svg/50px-Cricket_India_Crest.svg.png" width="50" height="50"/>
</Item>
</Section>
</SearchSuggestion>

I know that i can get the wiki text with the xml parse, but how? :-( or do i have to use httputils?

I hope you can help me a little bit, so that i can go further my self
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…