我正在学习如何进行XML解析,并收到了一份作业来解析如下所示的XML文件:
<?xml version="1.0" ?>
<deliveries>
<van id="VID-12345">
<package>
<product taxable="true" productName="Headphones" isbn="123456" unitPrice="10.00" quantity="1"/>
<product taxable="false" productName="Milk" isbn="234567" unitPrice="2.00" quantity="2"/>
<customer lastName="Adams" firstName="Maurice" streetAddress="123 4th St" zipCode="13126" accountNumber="ACCT-54321"/>
</package>
<package>
<product taxable="true" productName="Snickers" isbn="345678" unitPrice="1.00" quantity="1"/>
<product taxable="false" productName="Milk" isbn="234567" unitPrice="2.00" quantity="1"/>
<customer lastName="Baxter" firstName="Robert" streetAddress="234 5th St" zipCode="13126" accountNumber="ACCT-65432"/>
</package>
</van>
<cart id="VID-23456">
<package>
<product taxable="true" productName="Snickers" isbn="345678" unitPrice="1.00" quantity="1"/>
<customer lastName="Charles" firstName="Steven" streetAddress="345 6th St" zipCode="13126" accountNumber="ACCT-76543"/>
</package>
</cart>
</deliveries>
我需要解析它看起来像这样:
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
我如何解析它看起来像这样?我已经阅读了许多教程,但是它们要么使用非常复杂的XML,要么使用非常简单的XML作为示例,但是我认为它与创建列表和创建要解析的对象有关。我已经尝试了好几个小时才能找到解决方案,但是找不到正确的方法来解决。一个解决方案会很不错,但是即使是提示(以及教程链接)也将有助于指导我。我非常感谢您的帮助。这也是我到目前为止所得到的:
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("---=== Report ===---");
System.out.println("Van (" + attributes.getValue("id") + ")");
}
if (qName.equalsIgnoreCase("customer")) {
System.out.println(" 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") + ")");
System.out.println("---=== End of Report ===---");
}
}
}
结果(看起来确实错误):
---=== Report ===---
Van (VID-12345)
Customer
Adams, Maurice at 123 4th St, 13126
Customer
Baxter, Robert at 234 5th St, 13126
Cart (VID-23456)
---=== End of Report ===---
Customer
Charles, Steven at 345 6th St, 13126
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
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"));
}
}
}