gpt4 book ai didi

java - 如何使用 Stax 解析器读取相同的 xml 标签

转载 作者:行者123 更新时间:2023-11-30 07:26:50 25 4
gpt4 key购买 nike

<Product>
<SupplyDetail>
<Price>
<PriceTypeCode>01</PriceTypeCode>
<DiscountCoded>
<DiscountCodeType>02</DiscountCodeType>
<DiscountCodeTypeName>LSI</DiscountCodeTypeName>
<DiscountCode>25</DiscountCode></DiscountCoded>
<PriceAmount>29.95</PriceAmount>
<CurrencyCode>INR</CurrencyCode>
</Price>
</SupplyDetail>
<SupplyDetail>
<Price>
<PriceTypeCode>08</PriceTypeCode>
<PriceAmount>14.32</PriceAmount>
<CurrencyCode>INR</CurrencyCode>
</Price>
</SupplyDetail>
</Product>

我想要输出为

Pricetypecode  : 01
PriceAmount : 29.95
Currencycode : INR

Pricetypecode : 08
Price Amount : 14.32
Currencycode : INR

BulkFileXMLReader2.java

import com.main.Query.Query;
import com.main.Bean.PriceDetail;
import com.main.Bean.SpecificationBook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public static void main(String[] args) throws ClassNotFoundException, XMLStreamException, FileNotFoundException, SQLException {
String fileName = "D:\\SOFTWARE\\txt20160401.xml";
List<SpecificationBook> bookspec = (List<SpecificationBook>) parseXML(fileName);
for(SpecificationBook bean : bookspec){
System.out.println("The Price type code 08="+bean.priceTypeCode);
System.out.println("The price amount 08 is:"+bean.priceAmount);
System.out.println("The Currency Code 08 is:"+bean.currencyCode);
System.out.println("The price typecodechar 01 is:"+bean.priceTypeCodeChar);
System.out.println("The price amount 01 is:"+bean.priceAmount2);
System.out.println("The Currency code 01 is:"+bean.currencyCode1);
}
}
private static List<SpecificationBook> parseXML(String fileName) throws ClassNotFoundException, XMLStreamException {
List<SpecificationBook> empList = new ArrayList<>();
SpecificationBook emp = null;

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
try {
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));

while(xmlEventReader.hasNext()){
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()){
StartElement startElement = xmlEvent.asStartElement();
if(startElement.getName().getLocalPart().equals("Product")){

emp = new SpecificationBook();
}
else if(startElement.getName().getLocalPart().equals("PriceTypeCode")){
xmlEvent = xmlEventReader.nextEvent();

emp.setPriceTypeCode(xmlEvent.asCharacters().toString());
}
else
if(startElement.getName().getLocalPart().equals("PriceAmount")){
xmlEvent = xmlEventReader.nextEvent();
emp.setPriceAmount(xmlEvent.asCharacters().toString());
}
else if(startElement.getName().getLocalPart().equals("CurrencyCode")){
xmlEvent = xmlEventReader.nextEvent();
emp.setCurrencyCode(xmlEvent.asCharacters().toString());
}
}
if(xmlEvent.isEndElement()){
EndElement endElement = xmlEvent.asEndElement();

if(endElement.getName().getLocalPart().equals("Product")){
empList.add(emp);
}
}

}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return empList;
}

我的 Pojo 类(class)。 规范书.java

  public class SpecificationBook {

@Getter @Setter public String recordReference;
@Getter @Setter public String titleText;
@Getter @Setter public String imprintName;
@Getter @Setter public String publisherName;
@Getter @Setter public String illustrationDesc;
@Getter @Setter public String noofPages;
@Getter @Setter public String priceTypeCode;
@Getter @Setter public int book_id;
@Getter @Setter public String editionversionnumber;
@Getter @Setter public String recordreference1;
@Getter @Setter public String recordreference2;
@Getter @Setter public String recordreference3 ;
@Getter @Setter public String priceTypeCodeChar;
@Getter @Setter public String priceAmount;
@Getter @Setter public String priceAmount2;
@Getter @Setter public String currencyCode;
@Getter @Setter public String currencyCode1;
}

我尝试从供应详细信息 <price> 获取值标签值。运行此代码时。我从第二个 <SupplyDetail> 得到了值仅标签值。

在我的机器上运行此代码时,我得到的输出为

The Price type code 08=08
The price amount 08 is:14.83
The Currency Code 08 is:INR
The price typecodechar 01 is:null
The price amount 01 is:null
The Currency code 01 is:null

最佳答案

您永远不会调用 priceTypeCodeCharpriceAmount2currencyCode1 的 setter - 因此它们将始终打印 null

此外,还会为每个 Product 标记创建 SpecificationBook,并在遇到 Product 的结束标记时将其添加到结果列表中。这样,每个新的价格都会覆盖这些值。

解决此问题的最简单方法是更改​​您的 SpecificationBook 类,删除 Price 特定字段并添加 Price 对象列表.

class SpecificationBook {
// other fields
List<Price> prices;
}

其中 Price 类看起来像这样

class Price {
String priceAmount;
String priceTypeCode;
String currencyCode;
}

然后,您可以检查 Price 的开始标记,添加详细信息,当遇到 Price 结束标记时,将其添加到 SpecificationBook 价格

关于java - 如何使用 Stax 解析器读取相同的 xml 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36713428/

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