使用SAX解析器解析xml文件

时间:2017-03-10 20:05:02

标签: java xml

我的处理程序类:

   package csc241hw06;


        import java.time.format.DateTimeFormatter;
        import java.util.ArrayList;
        import java.util.List;
        import java.time.LocalDateTime;
        import java.time.ZoneOffset;

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

        /**
         * Created by Dan1 on 3/9/2017.
         */
public class MyHandler extends DefaultHandler {
    private Customer customer;
    private Account account;
    private Address address;
    private Meter meter;
    private MeterReading meterReading;
    private ArrayList <Customer> customerArrayList;
    private ArrayList <Address> addressList;

    String lastName;
    String firstName;
    String addressType;
    String accountNum;
    String accountType;

    public MyHandler() {
        customerArrayList = new ArrayList <Customer> ();
        addressList = new ArrayList <Address> ();
    }

    public ArrayList <Customer> getCustomerList() {
        return customerArrayList;
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ( qName.equalsIgnoreCase ( "Customer" ) ) {
            lastName = attributes.getValue ( "lastName" );
            firstName = attributes.getValue ( "firstName" );

        } else if ( qName.equalsIgnoreCase ( "account" ) ) {
            accountType = attributes.getValue ( "type" );
            accountNum = attributes.getValue ( "accountNumber" );
            if ( accountType.equalsIgnoreCase ( "residential" ) ) {
                account = new ResidentialAccount ( accountNum, customer );
            } else {
                account = new CommercialAccount ( accountNum, customer );
            }
        } else if ( qName.equalsIgnoreCase ( "address" ) ) {
            String unit = attributes.getValue ( "unit" );
            String street = attributes.getValue ( "street" );
            String number = attributes.getValue ( "number" );
            String zipCode = attributes.getValue ( "zipCode" );
            int parseNum = Integer.parseInt ( number );

            if ( attributes.getValue ( "type" ).equals ( "mailing" ) ) {
                if ( attributes.getValue ( "unit" ) == null ) {


                    if ( accountType.equalsIgnoreCase ( "residential" ) ) {
                        address = new Residence ( parseNum, street, zipCode );
                    } else {
                        address = new Commercial ( parseNum, street, zipCode );

                    }


                } else {
                    address = new Apartment ( parseNum, street, zipCode, unit );
                }
                if ( customer == null ) {
                    customer = new Customer ( lastName, firstName, address );
                }

            } else if ( attributes.getValue ( "type" ).equalsIgnoreCase ( "apartment" ) ) {
                address = new Apartment ( parseNum, street, zipCode, unit );


            } else if ( attributes.getValue ( "type" ).equalsIgnoreCase ( "commercial" ) ) {
                address = new Commercial ( parseNum, street, zipCode );
            } else if ( attributes.getValue ( "type" ).equalsIgnoreCase ( "house" ) ) {
                address = new Residence ( parseNum, street, zipCode );
            }


        } else if ( qName.equalsIgnoreCase ( "meter" ) ) {
            String id = attributes.getValue ( "id" );
            String brand = attributes.getValue ( "brand" );
            String meterType = attributes.getValue ( "type" );
            String location = attributes.getValue ( "location" );

            if ( meterType.equalsIgnoreCase ( "push" ) ) {
                meter = new PushMeter ( id, brand );
            } else if ( meterType.equalsIgnoreCase ( "poll" ) ) {
                meter = new PollMeter ( id, brand );
            }
            meter.setLocation ( address, location );


        } else if ( qName.equalsIgnoreCase ( "meterReading" ) ) {
            String reading = attributes.getValue ( "reading" );

            String date = attributes.getValue ( "date" );
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm" );
            LocalDateTime dateTime = LocalDateTime.parse ( date, formatter );

            String flag = attributes.getValue ( "flag" );

            meterReading = new MeterReading ( Double.parseDouble ( reading ), dateTime, flag, meter );
        }

    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ( qName.equalsIgnoreCase ( "customer" ) ) {
            customerArrayList.add ( customer );
        } else if ( qName.equalsIgnoreCase ( "account" ) ) {
            customer.addAccount ( account );
            account.updateBalance ();
        } else if ( qName.equalsIgnoreCase ( "address" ) ) {


            addressList.add ( address );


            for (int i = 0; i < addressList.size (); i++) {
                account.addAddress ( addressList.get ( i ) );
            }


            addressList = new ArrayList <Address> ();


        } else if ( qName.equalsIgnoreCase ( "meter" ) ) {
            address.addMeter ( meter );
        } else if ( qName.equalsIgnoreCase ( "meterReading" ) ) {
            meter.addReading ( meterReading );
        }
    }


}

我的xml文件:

    <customer lastName ="Adams" firstName="Maurice">
        <address type="mailing" unit="308" street="W 4th St." number="56" zipCode="13126"/>
        <account type="residential" accountNumber="876-543-21">
            <address type="apartment" unit="308" street="W 4th St." number="56" zipCode="13126">


                <meter id = "RM-4877-X4" brand="GE" type="push" location = "East side of building">

                    <meterReading reading="650" date = "2016-12-31T00:31" flag="push"/>
                    <meterReading reading="675" date = "2017-01-01T00:37" flag="push"/>
                    <meterReading reading="622" date = "2017-01-02T00:38" flag="push"/>
                </meter>      
            </address>
        </account>     
    </customer>


    <customer lastName ="Costanza" firstName="George">
        <address type="mailing" street="E 5th St." number="65" zipCode="13126"/>
        <account type="commercial" accountNumber="765-432-10">
            <address type="commercial" street="Albany St." number="56" zipCode="13126">
                <meter id = "CM-7784-D1" brand="GE" type="poll" location = "North entrance">
                    <meterReading reading="5043" date = "2016-12-31T00:32" flag="poll"/>
                    <meterReading reading="6152" date = "2017-01-01T00:38" flag="poll"/>
                    <meterReading reading="7261" date = "2017-01-02T00:41" flag="poll"/>
                </meter>      
            </address>
            <address type="commercial" street="Oneida St." number="72" zipCode="13126">
                <meter id = "CM-7684-D2" brand="GE" type="push" location = "South end of basement">
                    <meterReading reading="650" date = "2016-12-31T00:21" flag="push"/>
                    <meterReading reading="760" date = "2017-01-01T00:23" flag="push"/>
                    <meterReading reading="781" date = "2017-01-02T00:25" flag="push"/>
                    <meterReading reading="722" date = "2017-01-03T00:27" flag="push"/>
                </meter>      
            </address>
        </account>     
    </customer>
</xml>

问题是,每次我尝试将地址添加到地址列表时,都会在endElement方法中返回错误。是的,这是一个家庭作业,我已经尝试了一切,并且不知道还有谁问(我的老师目前没什么帮助),非常感谢任何帮助,谢谢

0 个答案:

没有答案
相关问题