有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SAX解析器如何处理来自的错误数据。XML文件

我需要做一个程序,需要一段时间。xml文件,该文件使用Java中的SAX解析器来解析。xml文件,将其存储在arrayList中,然后调用方法以使用arrayList显示某些对象

我的程序需要能够处理给SAX解析器错误数据的用户,这样,如果它没有正在寻找的特定标记,那么它就不会中断。我需要它能够加载数据并使用“check”命令检查数据的完整性。例如,如果客户没有与其关联的帐户,程序将输出哪个客户没有帐户

下面,我为程序、处理程序和应用程序设置了任务。下面有错误数据的xml

计划的任务:

检查:此命令用于检查命名项的完整性。换句话说,它检查给定类型的所有条目是否正确。例如,如果命令为: 检查客户 该计划应列出所有没有任何帐户的客户(名字和姓氏)。相关命令包括: 检查帐户:列出任何没有关联地址的帐户号码 检查地址:列出任何没有相关仪表的地址 检查仪表:列出没有任何仪表读数的仪表id,或其读数与仪表类型不匹配的仪表id,例如,轮询仪表的推送读数

。xml文件:

 <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>

处理程序文件:

    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;
        }
    }
}

谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    您可以再添加一个“坏”客户列表,如:

    // Variables to hold current values
    private ArrayList<Customer> customerList = new ArrayList<Customer>();
    private ArrayList<Customer> badCustomerList = new ArrayList<Customer>();
    ...
    

    并添加一些更改,将这些客户从“好”客户中分离出来。例如:

    @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;
    }
    

    实际上,您可以根据需要以不同的方式实现isCustomerGood。现在您只需在解析结束时运行check方法

  2. # 2 楼答案

    首先,我认为坏数据并不是指导致解析异常的非良好格式的XML文件

    如果上述假设是正确的,那么我认为您应该考虑使用XPath来查询数据文件并检查目标元素不存在的条件…

    那么,为什么不使用XPath呢?XPath可以使代码更易于编写和维护