gpt4 book ai didi

java - SAX 解析器 - 如何处理 .XML 文件中的错误数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:23 25 4
gpt4 key购买 nike

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

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

下面,我将程序的任务、处理程序和包含错误数据的 .xml 放在下面。

该计划的任务:

check:该命令用于检查指定条目的完整性。换句话说,它检查给定类型的所有条目是否正确。例如,如果命令是:检查客户该程序应列出没有任何帐户的所有客户(名字和姓氏)。相关命令包括:检查帐户:列出没有关联地址的任何帐号检查地址:列出没有关联仪表的任何地址检查仪表:列出没有任何仪表读数或其读数与仪表类型不匹配的任何仪表 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;
}
}
}

谢谢!

最佳答案

您可以再添加一个包含“不良”客户的列表,例如:

// 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 方法即可。

关于java - SAX 解析器 - 如何处理 .XML 文件中的错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36485811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com