Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.math.BigDecimal;
- import java.util.LinkedList;
- import java.util.List;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- import javax.xml.bind.Unmarshaller;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.adapters.XmlAdapter;
- import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
- /**
- * Parses the
- * <a href="http://www.w3schools.com/xml/xml_examples.asp">Plant Catalog XML</a>
- * <br /><br />
- *
- * Shows one method of parsing money, if you're not able to break the currencies
- * and values out into separate XML elements.
- *
- * @author RedTeaHacker
- */
- @XmlRootElement(name="CATALOG")
- public class PlantXml {
- public static void main(String[] args) throws JAXBException {
- if(args.length < 1) {
- System.out.println("Usage: java PlantXml <input_file> (<output_file>)");
- return;
- }
- // Read
- JAXBContext context = JAXBContext.newInstance(PlantXml.class);
- Unmarshaller unmarsh = context.createUnmarshaller();
- PlantXml plantXml = (PlantXml)unmarsh.unmarshal(new File(args[0]));
- // Output
- for(Plant plant: plantXml.plants) {
- System.out.printf("%20s |%25s |%7s |%13s |%6s |%7s\n"
- ,plant.common,plant.botanical,plant.zone,plant.light,plant.price,plant.availability);
- }
- if(args.length > 1) {
- // New
- plantXml = new PlantXml();
- plantXml.plants = new LinkedList<Plant>();
- plantXml.plants.add(new Plant("North Tower Flower","Lobelia cardinalis","99","Sunny",new Money("$",new BigDecimal("0.05")),"000001"));
- plantXml.plants.add(new Plant("Beethovenfoil","Potentilla","747","Shade",new Money("$",new BigDecimal("1000")),"011440"));
- // Write
- Marshaller marsh = context.createMarshaller();
- marsh.setProperty("jaxb.formatted.output",Boolean.TRUE);
- marsh.marshal(plantXml,new File(args[1]));
- }
- }
- @XmlElement(name="PLANT")
- public List<Plant> plants;
- public static class Plant {
- @XmlElement(name="COMMON")
- public String common;
- @XmlElement(name="BOTANICAL")
- public String botanical;
- @XmlElement(name="ZONE")
- public String zone;
- @XmlElement(name="LIGHT")
- public String light;
- @XmlElement(name="PRICE")
- @XmlJavaTypeAdapter(MoneyAdapter.class)
- public Money price;
- @XmlElement(name="AVAILABILITY")
- public String availability;
- public Plant() {
- }
- // Not necessary for JAXB, just helpful
- public Plant(String common,String botanical,String zone,String light,Money price,String availability) {
- this.common = common;
- this.botanical = botanical;
- this.zone = zone;
- this.light = light;
- this.price = price;
- this.availability = availability;
- }
- }
- public static class Money {
- public String currency = "$"; // Should probably use the Currency class
- public BigDecimal value = BigDecimal.ZERO;
- public Money() {
- }
- // Not necessary for JAXB, just helpful
- public Money(String currency,BigDecimal value) {
- this.currency = currency;
- this.value = value;
- }
- public String toString() {
- return currency + value.toPlainString();
- }
- }
- // You could combine this with the Money class
- public static class MoneyAdapter extends XmlAdapter<String,Money> {
- public String marshal(Money m) {
- return (m != null) ? m.toString() : "";
- }
- public Money unmarshal(String s) {
- Money m = new Money();
- // Currency
- StringBuilder b = new StringBuilder();
- int i = 0;
- for(; i < s.length(); ++i) {
- char c = s.charAt(i);
- if(Character.isWhitespace(c)) {
- continue; // ignore
- }
- if(Character.isDigit(c) || c == '-' || c == '.') {
- break; // done
- }
- b.append(c);
- }
- m.currency = b.toString();
- // Value
- b = new StringBuilder();
- for(; i < s.length(); ++i) {
- char c = s.charAt(i);
- if(!Character.isWhitespace(c)) {
- b.append(c);
- }
- }
- try {
- m.value = new BigDecimal(b.toString());
- }
- catch(Exception ex) {
- m.value = BigDecimal.ZERO;
- }
- return m;
- }
- }
- }
Add Comment
Please, Sign In to add comment