2 回答

TA貢獻1812條經(jīng)驗 獲得超5個贊
像這樣寫:
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"));
}
}
}
如果您不想要該Customers行(如果沒有),那么您需要跟蹤是否已經(jīng)打印了該行,例如:
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"));
}
}
}
輸出(來自上述兩個版本)
---=== 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 ===---

TA貢獻1789條經(jīng)驗 獲得超10個贊
看起來你已經(jīng)掌握了大部分內容。只需刪除一些打印線,它應該看起來像你想要的樣子。
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") + ")");
}
}
}
結果應該是這樣的:
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
您添加了額外的 println ,其中添加了額外的 "------====== blah ======-----" 。另外,由于 "println("customer") 位于 if 語句內部,因此每次 qName 等于 customer 時它都會打印它。因此將其放在外面,這樣它看起來與您正在尋找的產(chǎn)品相似!
添加回答
舉報