gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:25:57 24 4
gpt4 key购买 nike

我正在学习如何进行 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 {
@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 ===---

关于java - 我需要有关如何正确解析的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58348179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com