我需要有关如何正确解析的帮助

我正在学习如何进行 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 作为示例,但我认为这与创建列表和创建要解析的对象有关。我已经尝试了很多小时来寻找解决方案,但无法找到正确的方法。一个解决方案会很好,但即使是一个提示(和教程链接)也有助于指导我。我真的很感谢任何帮助。这也是我到目前为止所得到的:

沧海一幻觉
浏览 117回答 2
2回答

ABOUTYOU

像这样写:public class MyHandler extends DefaultHandler {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void startDocument() throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---=== Report ===---");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void endDocument() throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---=== End of Report ===---");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("van")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Van (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; Customers");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("cart")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Cart (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; Customers");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("customer")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; &nbsp; &nbsp; " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如果您不想要该Customers行(如果没有),那么您需要跟踪是否已经打印了该行,例如:public class MyHandler extends DefaultHandler {&nbsp; &nbsp; private boolean firstCustomer;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void startDocument() throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---=== Report ===---");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void endDocument() throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("---=== End of Report ===---");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("van")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Van (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstCustomer = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("cart")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Cart (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstCustomer = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("customer")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (firstCustomer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstCustomer = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; Customers");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; &nbsp; &nbsp; " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出(来自上述两个版本)---=== Report ===---Van (VID-12345)&nbsp; &nbsp; Customers&nbsp; &nbsp; &nbsp; &nbsp; Adams, Maurice at 123 4th St, 13126&nbsp; &nbsp; &nbsp; &nbsp; Baxter, Robert at 234 5th St, 13126Cart (VID-23456)&nbsp; &nbsp; Customers&nbsp; &nbsp; &nbsp; &nbsp; Charles, Steven at 345 6th St, 13126---=== End of Report ===---

至尊宝的传说

看起来你已经掌握了大部分内容。只需删除一些打印线,它应该看起来像你想要的样子。public class MyHandler extends DefaultHandler {&nbsp; &nbsp; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("van")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Van (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; Customer");&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("customer")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; &nbsp; &nbsp; " + attributes.getValue("lastName") + ", " + attributes.getValue("firstName") + " at " + attributes.getValue("streetAddress") + ", " + attributes.getValue("zipCode"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("cart")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Cart (" + attributes.getValue("id") + ")");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}结果应该是这样的:Van (VID-12345)&nbsp; &nbsp; Customers&nbsp; &nbsp; &nbsp; &nbsp; Adams, Maurice at 123 4th St, 13126&nbsp; &nbsp; &nbsp; &nbsp; Baxter, Robert at 234 5th St, 13126Cart (VID-23456)&nbsp; &nbsp; Customers&nbsp; &nbsp; &nbsp; &nbsp; Charles, Steven at 345 6th St, 13126您添加了额外的 println ,其中添加了额外的 "------====== blah ======-----" 。另外,由于 "println("customer") 位于 if 语句内部,因此每次 qName 等于 customer 时它都会打印它。因此将其放在外面,这样它看起来与您正在寻找的产品相似!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java