<Product>
<Row1>97545214</Row1>
<Row2>
<value>01</value>
</Row2>
<Row4>
<number>1</number>
<role>A01</role>
<name>Paul </name>
</Row4>
<Row4>
<number>1</number>
<role>A01</role>
<name>James </name>
</Row4>
<Row4>
<number>1</number>
<role>A01</role>
<name>John </name>
</Row4>
<Row5>
<Code>01</Code>
<Measurement>9.00</Measurement>
<UnitCode>in</UnitCode>
</Row5>
<Row5>
<Code>02</Code>
<Measurement>6.00</Measurement>
<UnitCode>in</UnitCode>
</Row5>
<Row5>
<Code>03</Code>
<Measurement>1.09</Measurement>
<UnitCode>in</UnitCode>
</Row5>
<Row7>
<price>
<Code>01</PriceTypeCode>
<Amount>62.95</Amount>
<currency>USD</currency>
</Price>
</Row7>
<Row7>
<price>
<Code>01</PriceTypeCode>
<Amount>62.95</Amount>
<currency>USD</currency>
</Price>
如何在java StaX解析器中读取此xml。这是示例xml。原始文件大小超过2 GB。所以只有我去StaX解析器。 我的Java类是 BulkFileReader.java
public class BulkFileReader {
public static void main(String[] args) throws ClassNotFoundException, XMLStreamException, FileNotFoundException {
String fileName = "E:\\Arunselvan\\D2 to D5\\xml files\\combine.xml";
List<BookSpecBean> bookspec = (List<BookSpecBean>) parseXML(fileName);
for(BookSpecBean bean : bookspec){
System.out.println("The Row1="+bean.row1);
System.out.println("The Row2="+bean.row2);
System.out.println("The Number="+bean.number);
System.out.println("The Role="+bean.role);
System.out.println("The Name="+bean.name);
System.out.println("The code="+bean.code);
System.out.println("The amount="+bean.amount);
System.out.println("The currency="+bean.currency);
System.out.println("===========================================");
new Query().InsertMetaData1(bean);
}
System.out.println("XML Completed Successfully");
}
private static List<BookSpecBean> parseXML(String fileName) {
List<BookSpecBean> empList = new ArrayList<>();
BookSpecBean 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 BookSpecBean();
}
}
//set the other varibles from xml elements
else if(startElement.getName().getLocalPart().equals("Row1")){
xmlEvent = xmlEventReader.nextEvent();
emp.setRow1(xmlEvent.asCharacters().getData());
}
else if(startElement.getName().getLocalPart().equals("Row2")){
xmlEvent = xmlEventReader.nextEvent();
emp.setRow2(xmlEvent.asCharacters().getData().replace("'", ""));
}
String qname = startElement.getName().getLocalPart();
if(qname.equalsIgnoreCase("Row4")){
xmlEvent = xmlEventReader.nextEvent();
}
else if(startElement.getName().getLocalPart().equals("number")){
xmlEvent = xmlEventReader.nextEvent();
emp.setnumber(xmlEvent.asCharacters().getData());
}
else if(startElement.getName().getLocalPart().equals("role")){
xmlEvent = xmlEventReader.nextEvent();
emp.setrole(xmlEvent.asCharacters().getData());
}
else if(startElement.getName().getLocalPart().equals("name")){
xmlEvent = xmlEventReader.nextEvent();
emp.setname(xmlEvent.asCharacters().getData());
}
}
if(xmlEvent.isEndElement()){
EndElement endElement = xmlEvent.asEndElement();
if(endElement.getName().getLocalPart().equals("Row4")){
empList.add(emp);
}
}
else if(startElement.getName().getLocalPart().equals("code")){
xmlEvent = xmlEventReader.nextEvent();
emp.setcode(xmlEvent.asCharacters().toString());
}
else if(startElement.getName().getLocalPart().equals("Amount")){
xmlEvent = xmlEventReader.nextEvent();
emp.setamount(xmlEvent.asCharacters().getData());
}
else if(startElement.getName().getLocalPart().equals("currency")){
xmlEvent = xmlEventReader.nextEvent();
emp.setcurrency(xmlEvent.asCharacters().getData());
}
}
//if Employee end element is reached, add employee object to list
if(xmlEvent.isEndElement()){
EndElement endElement = xmlEvent.asEndElement();
if(endElement.getName().getLocalPart().equals("Product")){
empList.add(emp);
}
}
}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return empList;
}
}
我使用此java代码从xml标记中检索值。当我使用此代码时。我可以检索第三个<row4>
值。剩余的<row4>
代码值无法检索。请帮助我获取所有<row4>
代码和<row7>
提前感谢您回答这个问题..
答案 0 :(得分:0)
如果您正在使用JaxB
,,则不需要自己解析XML文件,这就是JaxB
的原因对于! :)
使用JaxB
/ Unmarshaller
和XSD
XSD
结构的有效 XML
文件。XSD
文件和auto-generate JAXB classes。使用Unmarshaller
从XML文件填充自动生成的类:
JAXBContext jc = JAXBContext.newInstance(Product.class);
String fileName = "E:\\Arunselvan\\D2 to D5\\xml files\\combine.xml";
Unmarshaller u = jc.createUnmarshaller();
Product product = (Product) u.unmarshal(new FileInputStream(filename));
它...... JaxB
将负责类,属性,填充,写/读xml。