I have an xml file as
<?xml version="1.0" encoding="utf-8"?>
<sections>
<section>
<name>Most Pouplar</name>
<items>
<item pos="1">
<name>
AcaiBerry Diet
</name>
<description>
<![CDATA[
Natrol AcaiBerry Diet supports weight loss goals when combined with a healthy reduced-calorie diet and exercise program. Acai is a wild fruit harvested in the rain forests of Brazil recognized for its high ORAC (oxygen-radical absorbance capacity) value - a measure of its antioxidant capacity. An adequate intake of antioxidants helps neutralize harmful free radicals that are produced by the body as a result of regular exercise.
]]>
</description>
</item>
<item pos="2">
<name>
AcaiBerry Weekend Cleanse
</name>
<description>
<![CDATA[
AcaiBerry Weekend Cleanse is a 3-step, easy-to-use cleansing program. Step 1 helps minimize occasional constipation/bloating, step 2 helps reduce toxins via antioxidant protection & cell regeneration and step 3 helps to restore the friendly bacteria that protect & strengthen the GI tract.
]]>
</description>
</item>
<item pos="4">
<name>
Carb Intercept Phase 2 + Chromium
</name>
<description>
<![CDATA[
Natrol Carb Intercept supports a low-carb lifestyle by controlling carbohydrates found in breads, cereals, rice, pasta and other starch-containing foods. Each serving provides 1,000mg of Phase 2 Carb Controller; a clinically tested ingredient that inhibits the enzyme responsible for digesting starch into simple sugars your body can absorb.
]]>
</description>
</item>
<item pos="3">
<name>
Resveratrol Diet
</name>
<description>
<![CDATA[
Losing weight has never been so rejuvenating! Natrol introduces Resveratrol Diet, a complex blend of antioxidants, enzymes and other nutrientsto help boost your metabolism and promote calorie burning.
]]>
</description>
</item>
</items>
</section>
<section>
<name>Least Popular</name>
<items>
<item pos="1">
<name>
Advanced Sleep Melatonin 10mg Maximum Strength
</name>
<description>
<![CDATA[
Getting a good night's sleep is even easier with Natrol Melatonin - a natural nightcap. A hormone found in the body, melatonin, helps promote more restful sleep. Natrol Melatonin provides relief for occasional sleeplessness, and helps promote a more relaxing night and better overall health.
]]>
</description>
</item>
<item pos="2">
<name>
Sleep 'N Restore
</name>
<description>
<![CDATA[
If you need to feel more rested due to lack of sleep, try Natrol Sleep 'N Restore. Sleep 'N Restore helps promote a more restful, deeper sleep, while supporting your body's natural restoration processes.* A combination of melatonin and valerian, this natural sleep aide includes antioxidants that can help your body protect its cells from damage to help you restore and recharge while you sleep.
]]>
</description>
</item>
</items>
</section>
</sections>
I defined a POJO as
public class ItemPojo {
//Fields of an item
private String itemName;
private String itemDescription;
private int itemPosition;
//Getters and Setters
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public int getItemPosition() {
return itemPosition;
}
public void setItemPosition(int itemPosition) {
this.itemPosition = itemPosition;
}
}
I am implementing a method for parsing xml file, but I have no idea about how can I read multiple <item> tag, which are within <items> tag.
Edited
I am putting part of code, which I am trying
//Store all items with a particular section
ArrayList<ItemPojo> itemList = new ArrayList<ItemPojo>();
//Store all items categorized by section
Map<String, ArrayList<ItemPojo>> itemStore = new HashMap<String, ArrayList<ItemPojo>>(1);
//Single item
ItemPojo currentItem = null;
//Current section name
String sectionName = null;
public AndroidSaxFeedParser() {
super();
}
public void parse() { //Map<String, ArrayList<ItemPojo>>
RootElement root = new RootElement(SECTIONS);
Element section = root.getChild(SECTION);
Element itemHeader = section.getChild(ITEM_HEADER);
//Read <name> tag as used as section
itemHeader.setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
sectionName = body;
}
});
//TODO set item header here
Element items = section.getChild(ITEMS);
Element item = items.getChild(ITEM);
/*//Put all items of same category
items.setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
//sort item with position
Collections.sort(itemList, ItemPojo.COMPARE_BY_POSITION);
//Putting it into master list
itemStore.put(sectionName, itemList);
//And clear the item list
itemList.clear();
}
});*/
item.setStartElementListener(new StartElementListener() {
public void start(Attributes attributes) {
currentItem = new ItemPojo();
Log.i("Test xml", "item initalised " + currentItem.toString());
}
});
item.setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
// TODO Auto-generated method stub
itemList.add(currentItem);
Log.i("Test xml", "New items found " + currentItem.toString());
}
});
item.getChild(ITEM_NAME).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentItem.setItemName(body);
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentItem.setItemDescription(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
//return itemStore;
}
Now I am getting exception as
06-30 12:40:45.312: ERROR/AndroidRuntime(315): Uncaught handler: thread main exiting due to uncaught exception
06-30 12:40:45.342: ERROR/AndroidRuntime(315): java.lang.IllegalStateException: This element already has an end text element listener. It cannot have children.
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at android.sax.Element.getChild(Element.java:68)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at android.sax.Element.getChild(Element.java:60)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at org.us.ssg.AndroidSaxFeedParser.parse(AndroidSaxFeedParser.java:82)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at org.us.ssg.DesTestDemoActivity.checkXml(DesTestDemoActivity.java:109)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at org.us.ssg.DesTestDemoActivity.onClick(DesTestDemoActivity.java:81)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at android.view.View.performClick(View.java:2364)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at android.view.View.onTouchEvent(View.java:4179)
06-30 12:40:45.342: ERROR/AndroidRuntime(315): at android.widget.TextView.onTouchEvent(TextView.java:6541)
What I need
I need to read all items (with pos, name and description) and section. I am taking a HashMap, as key I am putting section and as value of that key I am puting an ArrayList of all items (with pos, name, description) related to that particular key (as section name).
You are well on your way. Next step is:
Define a startElementListener for you item element.
Like this:
item.setStartElementListener(new StartElementListener() {
#Override
public void start(Attributes attributes) {
myPojoItem = new PojoItem();
}
});
Define a endElementListener for you item element:
Like this:
item.setEndElementListener(new EndElementListener() {
#Override
public void end() {
itemList.add(myPojoItem);
}
});
For each of the children of item do something like the following:
itemName.setEndTextElementListener(new EndTextElementListener() {
#Override
public void end(String body) {
myPojoItem.setName(body);
}
});
finish with:
try {
Xml.parse(myXmlAsFileInputStream, Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
}
Update: In response to comment by OP, here is how to access attributes of elements:
item.setStartElementListener(new StartElementListener() {
#Override
public void start(Attributes attributes) {
position = attributes.getValue("pos");
}
});
Final solution
From OP : I have done it with these way
AndroidSaxFeedParser.java
.
public class AndroidSaxFeedParser extends BaseFeedParser {
//Store all items with a particular section
ArrayList<ItemPojo> itemList = new ArrayList<ItemPojo>();
//Store all items categorized by section
Map<String, ArrayList<ItemPojo>> itemStore = new HashMap<String, ArrayList<ItemPojo>>(1);
//Single item
ItemPojo currentItem = null;
//Current section name
String sectionName = null;
public AndroidSaxFeedParser() {
super();
}
public Map<String, ArrayList<ItemPojo>> parse() {
RootElement root = new RootElement(SECTIONS);
Element section = root.getChild(SECTION);
Element itemHeader = section.getChild(ITEM_HEADER);
//Read <name> tag as used as section
itemHeader.setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
sectionName = body.trim();
Log.i("New Section", "New section found : " + sectionName);
}
});
section.setStartElementListener(new StartElementListener() {
public void start(Attributes attributes) {
//Clear the item list
itemList = new ArrayList<ItemPojo>(0);
Log.i("Size of list", "Size : " +itemList.size());
}
});
section.setEndElementListener(new EndElementListener() {
public void end() {
//Putting it into master list
itemStore.put(sectionName, itemList);
}
});
Element items = section.getChild(ITEMS);
Element item = items.getChild(ITEM);
items.setEndElementListener(new EndElementListener() {
public void end() {
//sort item with position
Collections.sort(itemList, ItemPojo.COMPARE_BY_POSITION);
}
});
item.setStartElementListener(new StartElementListener() {
public void start(Attributes attributes) {
currentItem = new ItemPojo();
currentItem.setItemPosition(Integer.parseInt(attributes.getValue("pos")));
//Log.i("Test xml", "item initalised " + currentItem.toString());
}
});
item.setEndElementListener(new EndElementListener() {
public void end() {
itemList.add(currentItem);
Log.i("Test xml", "New items found " + currentItem.toString());
}
});
item.getChild(ITEM_NAME).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentItem.setItemName(body.trim());
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {
public void end(String body) {
currentItem.setItemDescription(body.trim());
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return itemStore;
}
}
.
public abstract class BaseFeedParser implements FeedParser {
// names of the XML tags
static final String SECTIONS = "sections";
static final String SECTION = "section";
static final String ITEM_HEADER = "name";
static final String DESCRIPTION = "description";
static final String ITEM_NAME = "name";
static final String ITEM_POSITION = "pos";
static final String ITEM = "item";
static final String ITEMS = "items";
public InputStream inStream;
public BaseFeedParser() {
//super();
}
protected InputStream getInputStream() {
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(DesTestDemoActivity.INDEX_URL);
HttpResponse response = null;
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("request_for", "xml_data"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null)
inStream = entity.getContent();
return inStream;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
.
public interface FeedParser {
Map<String, ArrayList<ItemPojo>> parse();
}
I have done with the help of you guys. So thank you all.
You don't seem to be using a SAX parser here but doing a DOM traveral.
If you want to parse the XML with a SAX parser, you need to initialize a SAXParser and define a ContentHandler where you will implement your parsing logic.
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(xmlFile);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
The ContentHandler has callbacks for when the parser reaches a start and end element. There you can put the logic required to fill up your POJO.
In your particular case, you need to check for the item tag and start filling up your POJOs.
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXMLHandler extends DefaultHandler {
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("sometag")) {
// process tag
}
}
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// handle end element
}
}
There are several articles on the web with full code samples for this. Just google for Android and SAX Parser.
One example where an XML file (containing item elements) is parsed using SAX can be found here.
Related
I'm currently trying to unmarshal some XML into a java object using JAXB and I'm getting a strange Null Pointer Exception. This is only an issue while unmarshalling. I can marshal with these classes just fine. Here are the relevant pieces of code (irrelevant portions are denoted by "..."):
The JAXB Root element:
...
#XmlRootElement(name="assets")
public class Assets {
String product;
Images images = new Images();
...
Public Assets() {}
...
public String getProduct() { return this.product; }
#XmlAttribute(name="product")
public void setProduct(String newProduct) { this.product = newProduct; }
public Images getImages() { return this.images; }
#XmlElement(name="images")
public void setImages(Images newImages) { this.images = newImages; }
...
}
The Images sub-element of the root element:
...
#XmlRootElement(name="images")
#XmlSeeAlso(Image.class)
#XmlType(propOrder = {"mainAsset", "image"})
public class Images {
List<Image> images;
Image mainAsset;
private static char counter = 'a';
private static final String prefix = "product-image-";
// Here's the part that causes the NPE
#XmlAnyElement(lax=true)
public List<JAXBElement<Image>> getImage() {
final List<JAXBElement<Image>> imageList = new ArrayList<JAXBElement<Image>>();
for (final Image g : this.images) {
imageList.add(new JAXBElement(new QName(prefix + counter++), Image.class, g));
}
counter = 'a';
return imageList;
}
#XmlElement(name="renditions")
public void setImage(List<Image> newImages) { this.images = newImages; }
public Image getMainAsset() { return this.mainAsset; }
#XmlElement(name="main-asset-name")
public void setMainAsset(Image newMainAsset) { this.mainAsset = newMainAsset; }
}
The logic for unmarshalling the XML:
...
public void modifyXML() {
try {
JAXBContext context = JAXBContext.newInstance(Assets.class, Images.class, Image.class);
Unmarshaller um = context.createUnmarshaller();
File f = new File("/path/to/my.xml");
Assets assets = (Assets) um.unmarshal(f);
...
} catch (JAXBException e) {
e.printStackTrace();
}
}
...
Finally, the XML I'm trying to unmarshal (it might help to know that this xml file was actually generated using the JAXB marshaller, which runs without any problems):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assets product="TEST PRODUCT">
<images>
<main-asset-name>
<path>stuff.jpg</path>
<title>theTitle</path>
<alt>theAlt</path>
<description>theDescription</description>
</main-asset-name>
<product-image-a>
<path>48x48.jpg</path>
<title>theTitle</path>
<alt>theAlt</path>
<description>theDescription</description>
</product-image-a>
<product-image-b>
<path>140x140.jpg</path>
<title>theTitle</path>
<alt>theAlt</path>
<description>theDescription</description>
</product-image-b>
<product-image-c>
<path>1280x1280.jpg</path>
<title>theTitle</path>
<alt>theAlt</path>
<description>theDescription</description>
</product-image-c>
<product-image-d>
<path>319x319.jpg</path>
<title>theTitle</path>
<alt>theAlt</path>
<description>theDescription</description>
</product-image-d>
</images>
</assets>
Okay, so that's all the relevant code (I think). When I run my program, I get the following error right after invoking the unmarshaller:
java.lang.NullPointerException
at com.my.companys.name.Images.getImage(Images.java:25)
And the line number referenced is the line where the for loop starts in my Images.java class file.
Does anyone have any ideas why this.images might be null here? Any help is greatly appreciated. Thanks ahead of time.
Here's the solution I've been using for now. It's a bit of a hack, but hell... It does the job.
First, I get the raw xml as a string instead of reading it as a file:
public void modifyXML() {
try {
JAXBContext context = JAXBContext.newInstance(Assets.class, Image.class);
Unmarshaller um = context.createUnmarshaller();
// This xml should be formatted in a way that JAXB can work with
String rawXML = getRawXML("/path/to/my.xml");
// Hand the modified xml to the unmarshaller
Assets umAssets = (Assets) um.unmarshal(new StreamSource(new StringReader(rawXML)));
// Do stuff with umAssets
...
} catch (Exception e) {
e.printStackTrace();
}
}
private String getRawXML(String path) throws IOException {
String xml = readFile(path, StandardCharsets.UTF_8);
// This should make it so JAXB can handle the xml properly
xml = xml.replaceAll("<main-asset-name>", "<image>");
xml = xml.replaceAll("</main-asset-name>", "</image>");
xml = xml.replaceAll("<product-image.*>", "<image>");
return xml.replaceAll("</product-image.*>", "</image>");
}
private String readFile(String path, Charset encoding) throws IOException {
// A simple file reader
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
This seems to do the trick, so I'm going to use this for now. Of course, I'm still open to better solutions if anyone has one.
I need to make a program that takes an .xml file that uses the SAX Parser in Java to parse the .xml file, store it in an arrayList and then call methods to display certain objects with the arrayList.
My program needs to be able to handle the user giving the SAX Parser bad data such that if it doesn’t have a certain tag it’s looking for, then it won’t break. I need it to be able to load the data and use a “check” command to check the intergrity of the data. For example, if the customer doest’t have an account associated with it, the program will output which customer doesn’t have an account.
Below, I’ve put the task for the program, the Handler, and the .xml with bad data below.
Task for the program:
check : This command is used to check the integrity of the named entries. In other words, it checks to see that all the entries of a given type are correct. For example, if the command is:
check customer
the program should list all customers (first name and last name) that do not have any accounts. Related commands include:
check account : list any account number without an associated address
check address : list any address without an associated meter
check meter : list any meter id without any meter readings, or whose readings do not match the meter type, e.g., push reading from a polling meter.
.xml File:
<xml version="1.0" encoding="UTF-8">
<!-- Customer with no account -->
<customer lastName ="Anderson" firstName="Thomas">
</customer>
<!-- Account with no address -->
<customer lastName ="Baker" firstName="Susanne">
<account type="residential" accountNumber="999-999-99">
</account>
</customer>
<!-- Address with no meter -->
<customer lastName ="Charles" firstName="Henry">
<account type="residential" accountNumber="888-888-88">
<address type="apartment" unit="308" street="E 6th St." number="56" zipCode="13126"/>
</account>
</customer>
<!-- Meter with no readings -->
<customer lastName ="Davidson" firstName="Mary">
<account type="residential" accountNumber="666-666-66">
<address type="apartment" unit="308" street="W 9th St." number="67" zipCode="13126">
<meter id = "RM-4876-X4" brand="GE" type="poll" location = "West side of building"/>
</address>
</account>
</customer>
<!-- Meter with mismatched readings -->
<customer lastName ="Evans" firstName="Oscar">
<account type="residential" accountNumber="555-555-55">
<address type="house" street="E 10th St." number="78" zipCode="13126">
<meter id = "RM-4874-X4" brand="GE" type="poll" location = "North side">
<meterReading reading="650" date = "1413227815" flag="poll"/>
<meterReading reading="675" date = "1413314215" flag="push"/>
<meterReading reading="622" date = "1413400615" flag="poll"/>
</meter>
</address>
</account>
</customer>
</xml>
Handler File:
package csc241hw07;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
// Variables to hold current values
private ArrayList<Customer> customerList = new ArrayList<Customer>();
private Customer currentCustomer;
private Account currentAccount;
private Address currentAddress;
private Meter currentMeter;
//getter method for employee list
public ArrayList<Customer> getCustList() {
return customerList;
}
#Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("customer")) {
//Create a customer object
String lastName = attributes.getValue("lastName");
String firstName = attributes.getValue("firstName");
currentCustomer = new Customer(lastName, firstName);
} else if (qName.equalsIgnoreCase("address")) {
// Create an Address object
String street = attributes.getValue("street");
int houseNumber = Integer.parseInt(attributes.getValue("number"));
String zipCode = attributes.getValue("zipCode");
String type = attributes.getValue("type");
String unit = attributes.getValue("unit");
if (type.equalsIgnoreCase("mailing")) {
// this is a mailing address -- assign to current customer
MailingAddress ma = new MailingAddress(street, houseNumber, zipCode, type);
currentCustomer.setMailingAddress(ma);
} else if (type.equalsIgnoreCase("house")) {
// Create a house
currentAddress = new House(street, houseNumber, zipCode, type);
} else if (type.equalsIgnoreCase("commercial")) {
// Create a commercial
currentAddress = new Commercial(street, houseNumber, zipCode, type);
} else if (unit != null) {
// Create an apartment
currentAddress = new Apartment(street, houseNumber, zipCode, type, unit);
} else {
System.out.println("Unknown address type:" + type);
}
if (currentAddress != null) {
// Assign this account to current address
currentAddress.setAccount(currentAccount);
currentAccount.addAddress(currentAddress);
}
} else if (qName.equalsIgnoreCase("meter")) {
// Create a meter object
String type = attributes.getValue("type");
String brand = attributes.getValue("brand");
String id = attributes.getValue("id");
if (type.equalsIgnoreCase("push")) {
currentMeter = new PushMeter(id, brand, type);
} else if (type.equalsIgnoreCase("poll")) {
currentMeter = new PollMeter(id, brand, type);
} else {
System.out.println("Unknown meter type: " + type);
}
if (currentMeter != null) {
// Set location
String location = attributes.getValue("location");
currentMeter.setLocation(currentAddress, location);
currentAddress.addMeter(currentMeter);
}
//System.out.println("METER:");
} else if (qName.equalsIgnoreCase("meterReading")) {
// Create a meter reading
//<meterReading reading="622" date = "1413400615" flag="push"/>
double reading = Double.parseDouble(attributes.getValue("reading"));
//System.out.println("DATE:" );
ZoneOffset z = ZoneOffset.ofHours(5);
long epoch = Long.parseLong(attributes.getValue("date"));
LocalDateTime d = LocalDateTime.ofEpochSecond(epoch,0,z);
//System.out.println("DATE:" + d.toString());
String flag = attributes.getValue("flag");
MeterReading mr = new MeterReading(reading, d, flag, currentMeter);
// Add this to current meter
currentMeter.addReading(mr);
//System.out.println("METERREADING:");
} else if (qName.equalsIgnoreCase("account")) {
// <account type="residential" accountNumber="876-543-21">
String type = attributes.getValue("type");
String acctNum = attributes.getValue("accountNumber");
if (type.equalsIgnoreCase("residential")) {
// residential account
currentAccount = new ResidentialAccount(acctNum, currentCustomer);
} else if (type.equalsIgnoreCase("commercial")) {
currentAccount = new CommercialAccount(acctNum, currentCustomer);
} else {
System.out.println("Unknown account type:" + type);
}
if (currentAccount != null) {
// Add this account to current customer
currentCustomer.addAccount(currentAccount);
}
}
}
#Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("customer")) {
customerList.add(currentCustomer);
currentCustomer = null;
} else if (qName.equalsIgnoreCase("meter")) {
currentMeter = null;
} else if (qName.equalsIgnoreCase("account")) {
currentAccount = null;
} else if (qName.equalsIgnoreCase("address")) {
currentAddress = null;
}
}
}
Thank you!
You can add one more list with "bad" customers like:
// Variables to hold current values
private ArrayList<Customer> customerList = new ArrayList<Customer>();
private ArrayList<Customer> badCustomerList = new ArrayList<Customer>();
...
And add some changes sorting those customers out of "good" ones. For instance:
#Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("customer")) {
if (isCustomerGood(currentCustomer)) { // Here is checking code
customerList.add(currentCustomer);
} else {
badCustomerList.add(currentCustomer);
}
currentCustomer = null;
} else if (qName.equalsIgnoreCase("meter")) {
currentMeter = null;
} else if (qName.equalsIgnoreCase("account")) {
currentAccount = null;
} else if (qName.equalsIgnoreCase("address")) {
currentAddress = null;
}
}
private static boolean isCustomerGood(Customer customer) {
return customer.getAccount() != null;
}
public boolean check() {
return badCustomerList.isEmpty();
}
public List<Customer> getBadCustomers() {
return badCustomerList;
}
Actually you can implement isCustomerGood differently depending on your needs. Now you just run check method at the end of parsing.
First of all, by bad data I dont think you mean a non-wellformed XML file that causes parsing exception.
If the above assumption is true, then I think you should consider using XPath to query the data file and check for the condition where the target element does not exist...
So why are you not using XPath which would make your code a lot easier to write and maintain?
I write manually a KML file trying to import some polygons in MyMaps. This way works fine:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Placemark>
<Style>
<PolyStyle>
<color>#a00000ff</color>
<outline>0</outline>
</PolyStyle>
</Style>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>9.184254,45.443636 9.183379,45.434288 9.224836,45.431499 9.184254,45.443636</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
I try to write a java program using JAK that generate a most possibile equal file, but it doesn't work with Maps
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:kml xmlns:atom="http://www.w3.org/2005/Atom" xmlns:ns3="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<ns3:Document>
<ns3:Placemark>
<ns3:Style>
<ns3:PolyStyle>
<ns3:color>#EABCFF</ns3:color>
<ns3:outline>0</ns3:outline>
</ns3:PolyStyle>
</ns3:Style>
<ns3:Polygon>
<ns3:innerBoundaryIs>
<ns3:LinearRing>
<ns3:coordinates>9.184254,45.443636 9.183379,45.434288 9.224836,45.431499 9.184254,45.443636</ns3:coordinates>
</ns3:LinearRing>
</ns3:innerBoundaryIs>
</ns3:Polygon>
</ns3:Placemark>
</ns3:Document>
</ns3:kml>
That's program:
public static void main(String[] args) throws IOException {
// Style
PolyStyle polystyle = KmlFactory.createPolyStyle();
polystyle.setColor("#EABCFF");
// polystyle.setFill(true);
polystyle.setOutline(false);
//
Kml kml = KmlFactory.createKml();
Document document = kml.createAndSetDocument();
Placemark pm = document.createAndAddPlacemark();
LinearRing linearRing = pm.createAndSetPolygon().createAndAddInnerBoundaryIs().createAndSetLinearRing();
linearRing.addToCoordinates(9.184254, 45.443636, 0);
linearRing.addToCoordinates(9.183379, 45.434288, 0);
linearRing.addToCoordinates(9.224836, 45.431499, 0);
linearRing.addToCoordinates(9.184254, 45.443636, 0);
pm.createAndAddStyle().setPolyStyle(polystyle);
//
kml.marshal(new FileWriter("D:/prova.kml"));
}
I view <ns3: in your kml this make the kml invalid for google maps
Try to correct the file
I had the same problem.
Instead of using kml.marshal(new FileWriter("D:/prova.kml")); I did this...
String name = kml.getClass().getSimpleName();
if ("Kml".equals(name)) {
name = name.toLowerCase();
}
JAXBContext jaxbContext = JAXBContext.newInstance(Kml.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NameSpaceBeautyfier());
JAXBElement<Kml> jaxbKml = new JAXBElement<>(new QName("http://www.opengis.net/kml/2.2", name), (Class<Kml>) kml.getClass(), kml);
jaxbMarshaller.marshal(jaxbKml, file);
With a NameSpaceBeautifier like this ...
private static final class NameSpaceBeautyfier extends NamespacePrefixMapper {
private static final String KML_PREFIX = ""; // DEFAULT NAMESPACE
private static final String KML_URI= "http://www.opengis.net/kml/2.2";
#Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(KML_URI.equals(namespaceUri)) {
return KML_PREFIX;
}
return suggestion;
}
#Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { KML_URI };
}
private NameSpaceBeautyfier() {
}
}
Hope this helps..
I am working with one application in which SAXparsing is placed. To get the City & State name from latitude and longitude I'm using Google API. Google API url google api
I want to get long_name short_name & type of header Tag address_component .
All the information I am getting successfully from this XML but problem is that when I am trying to get type Tag value . There are Two type tag in this header and I am always getting second type tag value .
Sample XML:
<address_component>
<long_name>Gujarat</long_name>
<short_name>Gujarat</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
How can I get type Tag value is administrative_area_level_1 as well as political?
I came across the following link which is really easy to give a start-
http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html
I add the data into one file named as location.xml(if you get this from web do your own logic for getting data after getting that data convert into Inputstream pass it to following code) i wrote a method in that you can get it
public void ReadAndWriteXMLFileUsingSAXParser(){
try
{
DefaultHandler handler = new MyHandler();
// parseXmlFile("infilename.xml", handler, true);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
InputStream rStream = null;
rStream = getClass().getResourceAsStream("location.xml");
saxParser.parse(rStream, handler);
}catch (Exception e)
{
System.out.println(e.getMessage());
}
}
This is MyHandler class. your final data stored into one vector called as "data"
class MyHandler extends DefaultHandler {
String rootname;Attributes atr;
private boolean flag=false;private Vector data;
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
rootname=localName;
atr=atts;
if(rootname.equalsIgnoreCase("address_component")){
data=new Vector();
flag=true;
}
}
public void characters(char[] ch, int start, int length){
String value=new String(ch,start,length);
if(flag)
{
if(rootname.equalsIgnoreCase("type")){
data.addElement(value) ;
System.out.println("++++++++++++++"+value);
}
if(rootname.equalsIgnoreCase("long_name")){
data.addElement(value) ;
System.out.println("++++++++++++++"+value);
}
if(rootname.equalsIgnoreCase("short_name")){
data.addElement(value) ;
System.out.println("++++++++++++++"+value);
}
}
}
public void endElement(String uri, String localName, String qName){
rootname=localName;
if(rootname.equalsIgnoreCase("address_component")){
flag=false;
}
}
}
you can find all data into the data vector and also you can find the data onconsole
as
++++++++++++++Gujarat
++++++++++++++Gujarat
++++++++++++++administrative_area_level_1
++++++++++++++political
Read this tutorial. This will help you to parse xml file using sax parser.
I have some HTML that I'm converting to a Spanned using Html.fromHtml(...), and I have a custom tag that I'm using in it:
<customtag id="1234">
So I've implemented a TagHandler to handle this custom tag, like so:
public void handleTag( boolean opening, String tag, Editable output, XMLReader xmlReader ) {
if ( tag.equalsIgnoreCase( "customtag" ) ) {
String id = xmlReader.getProperty( "id" ).toString();
}
}
In this case I get a SAX exception, as I believe the "id" field is actually an attribute, not a property. However, there isn't a getAttribute() method for XMLReader. So my question is, how do I get the value of the "id" field using this XMLReader? Thanks.
Here is my code to get the private attributes of the xmlReader by reflection:
Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
elementField.setAccessible(true);
Object element = elementField.get(xmlReader);
Field attsField = element.getClass().getDeclaredField("theAtts");
attsField.setAccessible(true);
Object atts = attsField.get(element);
Field dataField = atts.getClass().getDeclaredField("data");
dataField.setAccessible(true);
String[] data = (String[])dataField.get(atts);
Field lengthField = atts.getClass().getDeclaredField("length");
lengthField.setAccessible(true);
int len = (Integer)lengthField.get(atts);
String myAttributeA = null;
String myAttributeB = null;
for(int i = 0; i < len; i++) {
if("attrA".equals(data[i * 5 + 1])) {
myAttributeA = data[i * 5 + 4];
} else if("attrB".equals(data[i * 5 + 1])) {
myAttributeB = data[i * 5 + 4];
}
}
Note you could put the values into a map but for my usage that's too much overhead.
Based on the answer by rekire I made this slightly more robust solution that will handle any tag.
private TagHandler tagHandler = new TagHandler() {
final HashMap<String, String> attributes = new HashMap<String, String>();
private void processAttributes(final XMLReader xmlReader) {
try {
Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
elementField.setAccessible(true);
Object element = elementField.get(xmlReader);
Field attsField = element.getClass().getDeclaredField("theAtts");
attsField.setAccessible(true);
Object atts = attsField.get(element);
Field dataField = atts.getClass().getDeclaredField("data");
dataField.setAccessible(true);
String[] data = (String[])dataField.get(atts);
Field lengthField = atts.getClass().getDeclaredField("length");
lengthField.setAccessible(true);
int len = (Integer)lengthField.get(atts);
/**
* MSH: Look for supported attributes and add to hash map.
* This is as tight as things can get :)
* The data index is "just" where the keys and values are stored.
*/
for(int i = 0; i < len; i++)
attributes.put(data[i * 5 + 1], data[i * 5 + 4]);
}
catch (Exception e) {
Log.d(TAG, "Exception: " + e);
}
}
...
And inside handleTag do:
#Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
processAttributes(xmlReader);
...
And then the attributes will be accessible as so:
attributes.get("my attribute name");
It is possible to use XmlReader provided by TagHandler and get access to tag attribute values without reflection, but that method is even less straightforward than reflection. The trick is to replace ContentHandler used by XmlReader with custom object. Replacing ContentHandler can only be done in the call to handleTag(). That presents a problem getting attribute values for the first tag, which can be solved by adding a custom tag at the start of html.
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import java.util.ArrayDeque;
public class HtmlParser implements Html.TagHandler, ContentHandler
{
public interface TagHandler
{
boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes);
}
public static Spanned buildSpannedText(String html, TagHandler handler)
{
// add a tag at the start that is not handled by default,
// allowing custom tag handler to replace xmlReader contentHandler
return Html.fromHtml("<inject/>" + html, null, new HtmlParser(handler));
}
public static String getValue(Attributes attributes, String name)
{
for (int i = 0, n = attributes.getLength(); i < n; i++)
{
if (name.equals(attributes.getLocalName(i)))
return attributes.getValue(i);
}
return null;
}
private final TagHandler handler;
private ContentHandler wrapped;
private Editable text;
private ArrayDeque<Boolean> tagStatus = new ArrayDeque<>();
private HtmlParser(TagHandler handler)
{
this.handler = handler;
}
#Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)
{
if (wrapped == null)
{
// record result object
text = output;
// record current content handler
wrapped = xmlReader.getContentHandler();
// replace content handler with our own that forwards to calls to original when needed
xmlReader.setContentHandler(this);
// handle endElement() callback for <inject/> tag
tagStatus.addLast(Boolean.FALSE);
}
}
#Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
boolean isHandled = handler.handleTag(true, localName, text, attributes);
tagStatus.addLast(isHandled);
if (!isHandled)
wrapped.startElement(uri, localName, qName, attributes);
}
#Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if (!tagStatus.removeLast())
wrapped.endElement(uri, localName, qName);
handler.handleTag(false, localName, text, null);
}
#Override
public void setDocumentLocator(Locator locator)
{
wrapped.setDocumentLocator(locator);
}
#Override
public void startDocument() throws SAXException
{
wrapped.startDocument();
}
#Override
public void endDocument() throws SAXException
{
wrapped.endDocument();
}
#Override
public void startPrefixMapping(String prefix, String uri) throws SAXException
{
wrapped.startPrefixMapping(prefix, uri);
}
#Override
public void endPrefixMapping(String prefix) throws SAXException
{
wrapped.endPrefixMapping(prefix);
}
#Override
public void characters(char[] ch, int start, int length) throws SAXException
{
wrapped.characters(ch, start, length);
}
#Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
wrapped.ignorableWhitespace(ch, start, length);
}
#Override
public void processingInstruction(String target, String data) throws SAXException
{
wrapped.processingInstruction(target, data);
}
#Override
public void skippedEntity(String name) throws SAXException
{
wrapped.skippedEntity(name);
}
}
With this class reading attributes is easy:
HtmlParser.buildSpannedText("<x id=1 value=a>test<x id=2 value=b>", new HtmlParser.TagHandler()
{
#Override
public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes)
{
if (opening && tag.equals("x"))
{
String id = HtmlParser.getValue(attributes, "id");
String value = HtmlParser.getValue(attributes, "value");
}
return false;
}
});
This approach has the advantage that it allows to disable processing of some tags while using default processing for others, e.g. you can make sure that ImageSpan objects are not created:
Spanned result = HtmlParser.buildSpannedText("<b><img src=nothing>test</b><img src=zilch>",
new HtmlParser.TagHandler()
{
#Override
public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes)
{
// return true here to indicate that this tag was handled and
// should not be processed further
return tag.equals("img");
}
});
There's an alternative to the other solutions, that doesn't allow you to use custom tags, but has the same effect:
<string name="foobar">blah <annotation customTag="1234">inside blah</annotation> more blah</string>
Then read it like this:
CharSequence annotatedText = context.getText(R.string.foobar);
// wrap, because getText returns a SpannedString, which is not mutable
CharSequence processedText = replaceCustomTags(new SpannableStringBuilder(annotatedText));
public static <T extends Spannable> T replaceCustomTags(T text) {
Annotation[] annotations = text.getSpans(0, text.length(), Annotation.class);
for (Annotation a : annotations) {
String attrName = a.getKey();
if ("customTag".equals(attrName)) {
String attrValue = a.getValue();
int contentStart = text.getSpanStart(a);
int contentEnd = text.getSpanEnd(a);
int contentFlags = text.getSpanFlags(a);
Object newFormat1 = new StyleSpan(Typeface.BOLD);
Object newFormat2 = new ForegroundColorSpan(Color.RED);
text.setSpan(newFormat1, contentStart, contentEnd, contentFlags);
text.setSpan(newFormat2, contentStart, contentEnd, contentFlags);
text.removeSpan(a);
}
}
return text;
}
Depending on what you wanted to do with your custom tags, the above may help you. If you just want to read them, you don't need a SpannableStringBuilder, just cast getText to Spanned interface to investigate.
Note that Annotation representing <annotation foo="bar">...</annotation> is an Android built-in since API level 1! It's one of those hidden gems again. The It has the limitation of one attribute per <annotation> tag, but nothing prevents you from nesting multiple annotations to achieve multiple attributes:
<string name="gold_admin_user"><annotation user="admin"><annotation rank="gold">$$username$$</annotation></annotation></string>
If you use the Editable interface instead of Spannable you can also modify the content around each annotation. For example changing the above code:
String attrValue = a.getValue();
text.insert(text.getSpanStart(a), attrValue);
text.insert(text.getSpanStart(a) + attrValue.length(), " ");
int contentStart = text.getSpanStart(a);
will result as if you had this in the XML:
blah <b><font color="#ff0000">1234 inside blah</font></b> more blah
One caveat to look out for is when you make modifications that affect the length of the text, the spans move around. Make sure you read the span start/end indices at the correct times, best if you inline them to the method calls.
Editable also allows you to do simple search and replace substitution:
index = TextUtils.indexOf(text, needle); // for example $$username$$ above
text.replace(index, index + needle.length(), replacement);
If all you need is just one attribute the suggestion by vorrtex is actually pretty solid. To give you an example of just how simple it would be to handle have a look here:
<xml>Click on <user1>Johnni<user1> or <user2>Jenny<user2> to see...</<xml>
And in your custom TagHandler you don't use equals but indexOf
final static String USER = "user";
if(tag.indexOf(USER) == 0) {
// Extract tag postfix.
String postfix = tag.substring(USER.length());
Log.d(TAG, "postfix: " + postfix);
}
And you can then pass the postfix value in your onClick view parameter as a tag to keep it generic.