CVS Update: xmlpull-api-v1/src/java/samples
Aleksander Andrzej Slominski <[email protected]>
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
aslom 02/07/24 12:39:49
Modified: src/java/samples BestDeal.java README.txt
Log:
make sure that sample works and works correctly ...
Revision Changes Path
1.2 +40 -47 xmlpull-api-v1/src/java/samples/BestDeal.java
Index: BestDeal.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/samples/BestDeal.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -t -w -r1.1 -r1.2
--- BestDeal.java 2002/07/23 21:34:30 1.1
+++ BestDeal.java 2002/07/24 17:39:49 1.2
@@ -17,7 +17,6 @@
* properties we are collecting: best price, delivery time,
* product and vendor names
*/
-
public double price = Double.MAX_VALUE;
public int delivery = Integer.MAX_VALUE;
public String product = null;
@@ -26,33 +25,32 @@
/**
* target delivery value (refuse elements above this target)
*/
-
protected int targetDelivery;
/**
* creates an "empty" BestDeal with the given target for delivery
* @param td the target for delivery
*/
-
public BestDeal(int td) {
targetDelivery = td;
}
- /** updates the best deal from the given list in the format
+ /**
+ * updates the best deal from the given list in the format
*/
-
public void update(XmlPullParser parser)
throws IOException, XmlPullParserException {
parser.require(parser.START_DOCUMENT, null, null);
parser.nextTag();
+ parser.require(parser.START_TAG, NAMESPACE_URI, "price-list");
+
+ parser.nextTag();
parser.require(parser.START_TAG, NAMESPACE_URI, "name");
product = parser.nextText();
parser.require(parser.END_TAG, NAMESPACE_URI, "name");
- parser.nextTag();
- parser.require(parser.START_TAG, NAMESPACE_URI, "price-list");
while (parser.nextTag() == parser.START_TAG) {
checkVendor(parser);
@@ -65,36 +63,31 @@
}
/** subroutine handling a single vendor */
-
public void checkVendor(XmlPullParser parser)
throws IOException, XmlPullParserException {
parser.require(parser.START_TAG, NAMESPACE_URI, "vendor");
- parser.nextTag();
String currentVendor = null;
while (parser.nextTag() == parser.START_TAG) {
parser.require(parser.START_TAG, NAMESPACE_URI, null);
String name = parser.getName();
- if (name.equals("name"))
+ if (name.equals("name")) {
currentVendor = parser.nextText();
- else if (name.equals("price-quote")) {
+ } else if (name.equals("price-quote")) {
int currentDelivery =
Integer.parseInt(parser.getAttributeValue("", "delivery"));
double currentPrice = Double.parseDouble(parser.nextText());
- if (currentDelivery < delivery && currentPrice < price) {
+ if (currentDelivery < targetDelivery && currentPrice < price) {
vendor = currentVendor;
price = currentPrice;
delivery = currentDelivery;
}
+ } else {
+ System.out.println("trying to skip unknwon element: "+ name);
+ String content = parser.nextText();
+ System.out.println("skipped element content:" + content);
}
- else {
- System.err.println(
- "unknown element: "
- + name
- + " value: "
- + parser.nextText());
- }
parser.require(parser.END_TAG, NAMESPACE_URI, name);
}
parser.require(parser.END_TAG, NAMESPACE_URI, "vendor");
@@ -106,7 +99,6 @@
* @param args command-line argument
* @throw Exception catch-all for underlying exceptions
*/
-
public static void main(String[] args)
throws IOException, XmlPullParserException {
@@ -117,13 +109,13 @@
BestDeal bestDeal = new BestDeal(Integer.parseInt(args[1]));
- XmlPullParser parser =
- XmlPullParserFactory.newInstance().newPullParser();
+ XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
+ factory.setNamespaceAware(true);
+ XmlPullParser parser = factory.newPullParser();
InputStream is = new FileInputStream(args[0]);
parser.setInput(is, null);
bestDeal.update(parser);
- parser.setInput(null);
is.close();
Object[] objects =
@@ -137,3 +129,4 @@
}
}
+
1.7 +1 -1 xmlpull-api-v1/src/java/samples/README.txt
Index: README.txt
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/samples/README.txt,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -t -w -r1.6 -r1.7
--- README.txt 2002/04/18 23:19:45 1.6
+++ README.txt 2002/07/24 17:39:49 1.7
@@ -1 +1 @@
-place for samples demonstrating use of API
\ No newline at end of file
+place for samples demonstrating use of XMLPULL API