gpt4 book ai didi

java - XML SAX 解析

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:27 26 4
gpt4 key购买 nike

我必须打印此 XML 文件示例中每个客户的订单总额。

<OrderID id="10248">
<CustomerID>VINET</CustomerID>
<ProductName>Queso Cabrales</ProductName>
<UnitPrice>14.0000</UnitPrice>
<Quantity>12</Quantity>
<Freight>32.3800</Freight>
</OrderID>
<OrderID id="10248">
<CustomerID>VINET</CustomerID>
<ProductName>Singaporean Hokkien Fried Mee</ProductName>
<UnitPrice>9.8000</UnitPrice>
<Quantity>10</Quantity>
<Freight>32.3800</Freight>
</OrderID>
<OrderID id="10248">
<CustomerID>VINET</CustomerID>
<ProductName>Mozzarella di Giovanni</ProductName>
<UnitPrice>34.8000</UnitPrice>
<Quantity>5</Quantity>
<Freight>32.3800</Freight>
</OrderID>
<OrderID id="10249">
<CustomerID>TOMSP</CustomerID>
<ProductName>Tofu</ProductName>
<UnitPrice>18.6000</UnitPrice>
<Quantity>9</Quantity>
<Freight>11.6100</Freight>
</OrderID>
<OrderID id="10249">
<CustomerID>TOMSP</CustomerID>
<ProductName>Manjimup Dried Apples</ProductName>
<UnitPrice>42.4000</UnitPrice>
<Quantity>40</Quantity>
<Freight>11.6100</Freight>
</OrderID>

我想要的结果是这样的:

VINET:537.14

TOMSP:1886.62

等等

这些总计是根据结果建议的单价 * 数量 + 运费得出的。如果我给您 XML 的其余部分,您可以更清楚地看到它,但为了节省时间,我将其缩小了。我还得到一个错误,为这三个变量设置十进制格式,我不知道为什么。

public class DataProcessor2 extends DefaultHandler {

boolean unitPrice = false;
boolean collectCount = false;
boolean freight = false;;
boolean quantity = false;
boolean customer = false;

float currentCount = 0;
float totalCount = 0;
float unitPriceCount = 0;
float freightCount = 0;
float quantityCount = 0;

//unitprice, freight, quantity

public DataProcessor2(){
super();
}

public void startDocument() {
// TODO Auto-generated method stub
System.out.println("Order Totals Per Customer");
}

public void endDocument() {
// TODO Auto-generated method stub

System.out.println("Document END");
}

public void startElement(String namespaceUri, String localName,
String qualifiedName, Attributes attributes) {

DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
if (qualifiedName.equals("CustomerID")){
customer = true;
}
if(qualifiedName.equals("UnitPrice")){
unitPrice = true;
//unitPriceCount = Float.parseFloat((qualifiedName));
}
if(qualifiedName.equalsIgnoreCase("Freight")){
freight = true;
//freightCount = Float.parseFloat(df.format(qualifiedName));
}
if(qualifiedName.equalsIgnoreCase("Quantity")){
quantity = true;
//quantityCount = Float.parseFloat(df.format(qualifiedName));
}
if(unitPrice & freight & quantity){
collectCount = true;
//currentCount = unitPriceCount * quantityCount + freightCount;
}

}
public void endElement(String namespaceUri, String localName,
String qualifiedName, Attributes attributes) {
//System.out.println("End Element "+ qualifiedName);
}
public void characters (char[] ch, int start, int length) throws SAXException{

if (customer) {
System.out.println(new String(ch, start, length));
customer = false;
}
if(unitPrice){
// System.out.println(new String(ch, start, length));
unitPrice = false;
}
if(freight){
//System.out.println(new String(ch, start, length));
freight = false;
}
if(quantity){
//System.out.println(new String(ch, start, length));
quantity = false;
}
if(collectCount){
//System.out.println("Amount"+new String(ch, start, length));
collectCount = false;
}
if(unitPrice & freight & quantity){
collectCount = true;
//currentCount = unitPriceCount * quantityCount + freightCount;
}

}

}

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "UnitPrice"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at DataProcessor2.startElement(DataProcessor2.java:50)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at One.main(One.java:19)

最佳答案

下面是在 VTD-XML 中执行此操作的代码

import com.ximpleware.*;

public class xpathSearch {
private static double compute(VTDNav vn) throws VTDException{
double d1=0,d2=0;
if (vn.toElement(VTDNav.NEXT_SIBLING,"UnitPrice")){
int i=vn.getText();
if (i!=-1)
d1 = vn.parseDouble(i);
}
if (vn.toElement(VTDNav.NEXT_SIBLING,"Quantity")){
int i=vn.getText();
if (i!=-1)
d2 = vn.parseDouble(i);
}
return d1*d2;
}
public static void main(String s[])throws VTDException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("d:\\xml\\input2.txt", false))
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/root/OrderID");
int i;
double p1=0,p2 = 0;
while ((i=ap.evalXPath())!=-1){
if (vn.toElement(VTDNav.FIRST_CHILD,"CustomerID")){
i = vn.getText();
if (i!=-1) {
if(vn.matchTokenString(i, "TOMSP")){
p1 += compute(vn);
}
else if (vn.matchTokenString(i, "VINET")){

p2 += compute(vn);
}
}
vn.toElement(VTDNav.P);
}
}

System.out.println(" TOMSP "+p1);
System.out.println(" VINET "+p2);

}
}

关于java - XML SAX 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173223/

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