SAXXML文件解析(JAVA)
我必须创建一个类来从我创建的名为invoice.xml的xml中提取数据。在xml中,客户可以购买多个项目。打印提取的数据时的问题是,如果是这种情况,则只打印客户购买的最后一个元素。 有人能提出解决办法吗?我还将附加我的xml文件,因为我非常确定这就是问题所在 SAXParserExample.javaSAXXML文件解析(JAVA),java,xml,parsing,sax,invoice,Java,Xml,Parsing,Sax,Invoice,我必须创建一个类来从我创建的名为invoice.xml的xml中提取数据。在xml中,客户可以购买多个项目。打印提取的数据时的问题是,如果是这种情况,则只打印客户购买的最后一个元素。 有人能提出解决办法吗?我还将附加我的xml文件,因为我非常确定这就是问题所在 SAXParserExample.java package Attempt2; import java.io.IOException; import java.util.ArrayList; import java.util.Itera
package Attempt2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserExample extends DefaultHandler {
List<Invoice> myInvoices;
private String tempVal;
private Invoice tempInv;
public SAXParserExample() {
myInvoices = new ArrayList<Invoice>();
}
public void runExample() {
parseDocument();
printData();
}
private void parseDocument() {
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser sp = spf.newSAXParser();
sp.parse("invoice.xml", this);
} catch (SAXException se) {
se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
private void printData() {
System.out.println("\n\n\tNo of Invoices '" + myInvoices.size()
+ "'.\n");
Iterator<Invoice> it = myInvoices.iterator();
while (it.hasNext()) {
System.out.println("\n\t" + it.next().toString());
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tempVal = "";
if (qName.equalsIgnoreCase("Invoice")) {
tempInv = new Invoice();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("invoice")) {
myInvoices.add(tempInv);
} else if (qName.equalsIgnoreCase("invoicenum")) {
tempInv.setInvoiceNum(tempVal);
} else if (qName.equalsIgnoreCase("title")) {
tempInv.setTitle((tempVal));
} else if (qName.equalsIgnoreCase("name")) {
tempInv.setName((tempVal));
} else if (qName.equalsIgnoreCase("street")) {
tempInv.setStreet((tempVal));
} else if (qName.equalsIgnoreCase("city")) {
tempInv.setCity((tempVal));
} else if (qName.equalsIgnoreCase("county")) {
tempInv.setCounty((tempVal));
} else if (qName.equalsIgnoreCase("postal")) {
tempInv.setPostal((tempVal));
} else if (qName.equalsIgnoreCase("description")) {
tempInv.setDescription((tempVal));
} else if (qName.equalsIgnoreCase("price")) {
tempInv.setPrice((tempVal));
} else if (qName.equalsIgnoreCase("quantity")) {
tempInv.setQuantity((tempVal));
}
}
public static void main(String[] args) {
SAXParserExample spe = new SAXParserExample();
spe.runExample();
}
}
因为我完全误解了您的问题,并且整天把您引入歧途,所以我已经修复了您的解析器,使其将项目作为单独的对象处理,现在您的发票包含了项目列表。我道歉
public class Item {
String description;
String price;
String quantity;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Item");
sb.append("{description='").append(description).append('\'');
sb.append(", price='").append(price).append('\'');
sb.append(", quantity='").append(quantity).append('\'');
sb.append('}');
return sb.toString();
}
}
公共类发票{
字符串invoiceNum;
字符串标题;
字符串名;
弦街;;
字符串城市;
弦县;
字符串邮政;
清单项目;
公共发票(){
this.items=new ArrayList();
}
公共无效附加项(项目){
此.items.add(item);
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
this.city=城市;
}
公共字符串getCounty(){
回归县;
}
公营县(字符串县){
this.county=县;
}
公共字符串getInvoiceNum(){
返回发票编号;
}
公共void setInvoiceNum(字符串invoiceNum){
this.invoiceNum=invoiceNum;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getPostal(){
回邮;
}
公共邮政(字符串邮政){
这是邮政的;
}
公共字符串getStreet(){
返回街;;
}
公共街道(字符串街){
这条街;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
@凌驾
公共字符串toString(){
最终StringBuilder sb=新StringBuilder();
某人附上(“发票”);
sb.append(“{city=”)。append(city)。append(“\”);
sb.append(“,invoiceNum=”).append(invoiceNum.append(“\”);
追加(“,title=”)。追加(title)。追加(“\”);
sb.append(“,name=”)。append(name)。append(“\”);
附加(“,street=”)。附加(street)。附加(“\”);
sb.append(“,country=”)。append(country)。append(“\”);
某人追加(“,postally=”)。追加(postally)。追加(“\”);
sb.追加(“,items=”)。追加(items);
某人附加('}');
使某人返回字符串();
}
}
公共类SaxParserExample扩展了DefaultHandler{
列出我的发票;
私有字符串tempVal;
私人发票;
私人物品;
公共SAXParseRexSample(){
myInvoices=新的ArrayList();
}
public void运行示例(){
解析文档();
printData();
}
私有文档(){
SAXParserFactory spf=SAXParserFactory.newInstance();
试一试{
SAXParser sp=spf.newSAXParser();
sp.parse(“invoice.xml”,this);
}捕获(SAXSE异常){
se.printStackTrace();
}捕获(ParserConfiguration异常pce){
pce.printStackTrace();
}捕获(IOIE){
即printStackTrace();
}
}
私有void printData(){
System.out.println(“发票数量””+myInvoices.size());
用于(发票我的发票:我的发票){
系统输出打印项次(我的发票);
}
}
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
tempVal=“”;
如果(“发票”。相等信号案例(qName)){
tempInv=新发票();
}else if(“item”.equalsIgnoreCase(qName)){
tempItem=新项();
}
}
公共无效字符(字符[]ch,整数开始,整数长度)
抛出SAX异常{
tempVal=新字符串(ch、start、length);
}
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
如果(“发票”。相等信号案例(qName)){
myInvoices.add(tempInv);
}else if(“item”.equalsIgnoreCase(qName)){
临时投资附加项目(临时项目);
}else if(“invoicenum.equalsIgnoreCase(qName)){
tempInv.setInvoiceNum(tempVal);
}else if(“title.equalsIgnoreCase(qName)){
tempInv.setTitle((tempVal));
}else if(“name”.equalsIgnoreCase(qName)){
tempInv.setName((tempVal));
}else if(“street.”等信号案例(qName)){
临时投资设定值((临时值));
}else if(“城市”。相等信号案例(qName)){
tempInv.setCity((tempVal));
}else if(“县”。相等信号案例(qName)){
tempInv.setCounty((tempVal));
}else if(“postal”.equalsIgnoreCase(qName)){
tempInv.setPostal((tempVal));
}else if(“description”.equalsIgnoreCase(qName)){
tempItem.setDescription((tempVal));
}else if(“price”.equalsIgnoreCase(qName)){
tempItem.setPrice((tempVal));
}else if(“数量”。相等信号案例(qName)){
tempItem.setQuantity((tempVal));
}
}
公共静态void main(字符串[]args){
SAXParseReSample spe=新的SAXParseReSample();
spe.runExample();
}
}DTD说一个元素只能有一个元素,一个元素和一个元素。在XML文件中,有两个元素,这与DTD有关
如果可能有多个元素,一种解决方案是更改DTD的以下行
到
XML文件中的以下行也是错误的:
应该是
DTD中没有人员元素定义我建议您使用一个框架,比如Apache Digester,来读取XML文件,而不是自制的代码。很遗憾,项目要求我使用Java。Apache Digester是一个Java库。这里不需要使用库。他已经有了工作代码。那将是浪费时间。另外,他不用使用第三方库,只需使用JAXB,而不用手动构建SAX解析器。如果不需要,则无需引入第三方依赖关系。(JAXB是在Java1.6的第14次更新时添加到JDK的)DTD是否允许多个项目?我个人认为
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT invoices (invoice*)>
<!ELEMENT invoice (invoicenum, address, items)>
<!ELEMENT address (title, name, street, city, county, postal)>
<!ELEMENT items (order, item)>
<!ELEMENT item (description, price, quantity)>
<!ELEMENT invoicenum (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT county (#PCDATA)>
<!ELEMENT postal (#PCDATA)>
<!ELEMENT order (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
package Attempt2;
public class Invoice {
private String invoicenum;
private String title;
private String name;
private String street;
private String city;
private String county;
private String postal;
private String order;
private String description;
private String price;
private String quantity;
public Invoice() {
System.out.println("\n\t No Arg Invoice Constructor Called…");
}
public Invoice(String invoicenum, String title, String name, String street,
String city, String county, String postal, String order,
String description, String price, String quantity) {
System.out.println("\n\t 4-Argument Invoice Constructor Called…");
this.invoicenum = invoicenum;
this.title = title;
this.name = name;
this.street = street;
this.city = city;
this.county = county;
this.postal = postal;
this.order = order;
this.description = description;
this.price = price;
this.quantity = quantity;
}
public String getInvoiceNum() {
return invoicenum;
}
public void setInvoiceNum(String invoicenum) {
this.invoicenum = invoicenum;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// street
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
// city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
// county
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
// postal
public String getPostal() {
return postal;
}
public void setPostal(String postal) {
this.postal = postal;
}
// order
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
// description
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// price
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
// quantity
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
/**
private String title;
private String order;
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Invoice Details \n---------------------------------\n");
sb.append("Invoice Number:" + getInvoiceNum()+"\n");
sb.append("Name:" + getName()+"\n");
sb.append("Street:" + getStreet()+"\n");
sb.append("City:" + getCity()+"\n");
sb.append("County:" + getCounty()+"\n");
sb.append("Postal Code:" + getPostal()+"\n");
sb.append("Item Description:" + getDescription()+"\n");
sb.append("Price:" + getPrice()+"\n");
sb.append("Quantity:" + getQuantity()+"\n");
return sb.toString();
}
}
public class Item {
String description;
String price;
String quantity;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Item");
sb.append("{description='").append(description).append('\'');
sb.append(", price='").append(price).append('\'');
sb.append(", quantity='").append(quantity).append('\'');
sb.append('}');
return sb.toString();
}
public class Invoice {
String invoiceNum;
String title;
String name;
String street;
String city;
String county;
String postal;
List<Item> items;
public Invoice() {
this.items = new ArrayList<Item>();
}
public void addItem(Item item) {
this.items.add(item);
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getInvoiceNum() {
return invoiceNum;
}
public void setInvoiceNum(String invoiceNum) {
this.invoiceNum = invoiceNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPostal() {
return postal;
}
public void setPostal(String postal) {
this.postal = postal;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Invoice");
sb.append("{city='").append(city).append('\'');
sb.append(", invoiceNum='").append(invoiceNum).append('\'');
sb.append(", title='").append(title).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", street='").append(street).append('\'');
sb.append(", county='").append(county).append('\'');
sb.append(", postal='").append(postal).append('\'');
sb.append(", items=").append(items);
sb.append('}');
return sb.toString();
}
public class SaxParserExample extends DefaultHandler {
List<Invoice> myInvoices;
private String tempVal;
private Invoice tempInv;
private Item tempItem;
public SaxParserExample() {
myInvoices = new ArrayList<Invoice>();
}
public void runExample() {
parseDocument();
printData();
}
private void parseDocument() {
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser sp = spf.newSAXParser();
sp.parse("invoice.xml", this);
} catch (SAXException se) {
se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
private void printData() {
System.out.println("No of Invoices '" + myInvoices.size());
for (Invoice myInvoice : myInvoices) {
System.out.println(myInvoice);
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tempVal = "";
if ("Invoice".equalsIgnoreCase(qName)) {
tempInv = new Invoice();
} else if ("item".equalsIgnoreCase(qName)) {
tempItem = new Item();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if ("invoice".equalsIgnoreCase(qName)) {
myInvoices.add(tempInv);
} else if ("item".equalsIgnoreCase(qName)) {
tempInv.addItem(tempItem);
} else if ("invoicenum".equalsIgnoreCase(qName)) {
tempInv.setInvoiceNum(tempVal);
} else if ("title".equalsIgnoreCase(qName)) {
tempInv.setTitle((tempVal));
} else if ("name".equalsIgnoreCase(qName)) {
tempInv.setName((tempVal));
} else if ("street".equalsIgnoreCase(qName)) {
tempInv.setStreet((tempVal));
} else if ("city".equalsIgnoreCase(qName)) {
tempInv.setCity((tempVal));
} else if ("county".equalsIgnoreCase(qName)) {
tempInv.setCounty((tempVal));
} else if ("postal".equalsIgnoreCase(qName)) {
tempInv.setPostal((tempVal));
} else if ("description".equalsIgnoreCase(qName)) {
tempItem.setDescription((tempVal));
} else if ("price".equalsIgnoreCase(qName)) {
tempItem.setPrice((tempVal));
} else if ("quantity".equalsIgnoreCase(qName)) {
tempItem.setQuantity((tempVal));
}
}
public static void main(String[] args) {
SaxParserExample spe = new SaxParserExample();
spe.runExample();
}