第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要有關如何正確解析的幫助

我需要有關如何正確解析的幫助

滄海一幻覺 2023-10-19 14:52:28
我正在學習如何進行 XML 解析,并收到了一個解析 XML 文件的作業(yè),如下所示:<?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, 13126Cart (VID-23456)    Customers        Charles, Steven at 345 6th St, 13126我如何解析它看起來像這樣?我讀過很多教程,但它們要么使用非常復雜的 XML,要么使用非常簡單的 XML 作為示例,但我認為這與創(chuàng)建列表和創(chuàng)建要解析的對象有關。我已經(jīng)嘗試了很多小時來尋找解決方案,但無法找到正確的方法。一個解決方案會很好,但即使是一個提示(和教程鏈接)也有助于指導我。我真的很感謝任何幫助。這也是我到目前為止所得到的:
查看完整描述

2 回答

?
ABOUTYOU

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 ===---


查看完整回答
反對 回復 2023-10-19
?
至尊寶的傳說

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)品相似!


查看完整回答
反對 回復 2023-10-19
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號