2 回答
TA贡献1812条经验 获得超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行(如果没有),那么您需要跟踪是否已经打印了该行,例如:
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条经验 获得超10个赞
看起来你已经掌握了大部分内容。只需删除一些打印线,它应该看起来像你想要的样子。
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 时它都会打印它。因此将其放在外面,这样它看起来与您正在寻找的产品相似!
添加回答
举报