Firefly
Firefly

Reputation: 428

I need help on how to parse properly

I'm learning how to do XML parsing and received a homework to parse an XML file that look like this:

<?xml version="1.0" ?>
<deliveries>
    <van id="VID-12345">
        <package>
            <product taxable="true" productName="Headphones" isbn="123456" unitPrice="10.00" quantity="1"/>
            <product taxable="false" productName="Milk" isbn="234567" unitPrice="2.00" quantity="2"/>
            <customer lastName="Adams" firstName="Maurice" streetAddress="123 4th St" zipCode="13126" accountNumber="ACCT-54321"/>
        </package>
        <package>
            <product taxable="true" productName="Snickers" isbn="345678" unitPrice="1.00" quantity="1"/>
            <product taxable="false" productName="Milk" isbn="234567" unitPrice="2.00" quantity="1"/>
            <customer lastName="Baxter" firstName="Robert" streetAddress="234 5th St" zipCode="13126" accountNumber="ACCT-65432"/>
        </package>
    </van>
    <cart id="VID-23456">
        <package>
            <product taxable="true" productName="Snickers" isbn="345678" unitPrice="1.00" quantity="1"/>
            <customer lastName="Charles" firstName="Steven" streetAddress="345 6th St" zipCode="13126" accountNumber="ACCT-76543"/>
        </package>
    </cart>
</deliveries>

I need to parse it to look like this:

Van (VID-12345)
    Customers
        Adams, Maurice at 123 4th St, 13126
        Baxter, Robert at 234 5th St, 13126
Cart (VID-23456)
    Customers
        Charles, Steven at 345 6th St, 13126

How do I parse it to look like that? I've read many tutorials but they are either using a very complex XML or a very simple one as examples but I think it has something to do with creating a list and creating objects to parse. I've been trying for many hours for a solution but couldn't figure out a correct way to do it. A solution would be nice but even a hint (and a tutorial link) would also be helpful to guide me. I really appreciate any help. Also this is what I've got so far:

public class MyHandler extends DefaultHandler {

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (qName.equalsIgnoreCase("van")) {
            System.out.println("---=== Report ===---");
            System.out.println("Van (" + attributes.getValue("id") + ")");
        }
        if (qName.equalsIgnoreCase("customer")) {
            System.out.println("    Customer");
            System.out.println("        " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));
        }
        if (qName.equalsIgnoreCase("cart")) {
            System.out.println("Cart (" + attributes.getValue("id") + ")");
            System.out.println("---=== End of Report ===---");
        }
    }
}

The result (which looks really wrong):

---=== Report ===---
Van (VID-12345)
    Customer
        Adams, Maurice at 123 4th St, 13126
    Customer
        Baxter, Robert at 234 5th St, 13126
Cart (VID-23456)
---=== End of Report ===---
    Customer
        Charles, Steven at 345 6th St, 13126

Upvotes: 0

Views: 59

Answers (2)

Andreas
Andreas

Reputation: 159086

Write it like this:

public class MyHandler extends DefaultHandler {
    @Override
    public void startDocument() throws SAXException {
        System.out.println("---=== Report ===---");
    }
    @Override
    public void endDocument() throws SAXException {
        System.out.println("---=== End of Report ===---");
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("van")) {
            System.out.println("Van (" + attributes.getValue("id") + ")");
            System.out.println("    Customers");
        }
        if (qName.equalsIgnoreCase("cart")) {
            System.out.println("Cart (" + attributes.getValue("id") + ")");
            System.out.println("    Customers");
        }
        if (qName.equalsIgnoreCase("customer")) {
            System.out.println("        " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));
        }
    }
}

If you don't want the Customers line if there aren't any, then you need to track whether you printed the line already, e.g. like this:

public class MyHandler extends DefaultHandler {
    private boolean firstCustomer;
    @Override
    public void startDocument() throws SAXException {
        System.out.println("---=== Report ===---");
    }
    @Override
    public void endDocument() throws SAXException {
        System.out.println("---=== End of Report ===---");
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("van")) {
            System.out.println("Van (" + attributes.getValue("id") + ")");
            firstCustomer = true;
        }
        if (qName.equalsIgnoreCase("cart")) {
            System.out.println("Cart (" + attributes.getValue("id") + ")");
            firstCustomer = true;
        }
        if (qName.equalsIgnoreCase("customer")) {
            if (firstCustomer) {
                firstCustomer = false;
                System.out.println("    Customers");
            }
            System.out.println("        " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));
        }
    }
}

Output (from both versions above)

---=== Report ===---
Van (VID-12345)
    Customers
        Adams, Maurice at 123 4th St, 13126
        Baxter, Robert at 234 5th St, 13126
Cart (VID-23456)
    Customers
        Charles, Steven at 345 6th St, 13126
---=== End of Report ===---

Upvotes: 1

inf_loop
inf_loop

Reputation: 9

It looks like you've got most of it. Just remove some print lines and it should look like what you want it to look like.

public class MyHandler extends DefaultHandler {

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        if (qName.equalsIgnoreCase("van")) {
            System.out.println("Van (" + attributes.getValue("id") + ")");
        }
        System.out.println("    Customer");
        if (qName.equalsIgnoreCase("customer")) {
            System.out.println("        " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));
        }
        if (qName.equalsIgnoreCase("cart")) {
            System.out.println("Cart (" + attributes.getValue("id") + ")");
        }
    }
}

It should result like this:

Van (VID-12345)
    Customers
        Adams, Maurice at 123 4th St, 13126
        Baxter, Robert at 234 5th St, 13126
Cart (VID-23456)
    Customers
        Charles, Steven at 345 6th St, 13126

You added the extra println's which added the extra "------====== blah ======-----" 's. Also, since "println("customer") was inside of the if statement, it will print it everytime qName equals customer. Therefore put it on the outside so it will look similar to the product you are looking for!

Upvotes: 0

Related Questions