简体   繁体   English

如何在java中解析XML以使用STAX在db中保存值?

[英]How to parse XML in java to save values in db using STAX?

I am new to JAVA STAX Parser and I have to parse a xml to populate my database table. 我是JAVA STAX Parser的新手,我必须解析一个xml来填充我的数据库表。

While trying to read XML file using STAX I came across this problem. 在尝试使用STAX读取XML文件时,我遇到了这个问题。 In an XML file, I may have child nodes with the same name in different root nodes. 在XML文件中,我可能在不同的根节点中具有相同名称的子节点。 I couldn't quite figure out how to read specific child nodes from root nodes. 我无法弄清楚如何从根节点读取特定的子节点。

XML File Sample:- XML文件示例: -

<?xml version="1.0" encoding="UTF-8"?>
<DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XML.xsd">
<FMTR>
<TITLEPG>
  <TITLENUM>Title 1</TITLENUM>
  <SUBJECT>Test 1</SUBJECT>
</TITLEPG>
<BTITLE>
  <P></P>      
</BTITLE>
<TOC>
  <EXPL>
    <SUBJECT>Explanation</SUBJECT>
  </EXPL>
  <TITLENO>
    <CHAPTI>
      <SUBJECT>Chapter I—Test 1</SUBJECT>
    </CHAPTI>
  </TITLENO>
  <FAIDS>
    <SUBJECT>Table of Titles and Chapters</SUBJECT>
    <SUBJECT>Alphabetical List</SUBJECT>
  </FAIDS>
</TOC>
</FMTR>
</DOC>  

For eg:- I have to read the SUBJECT tag of TITLEPG root tag and populate the database table accordingly. 例如: -我必须读取TITLEPG根标记的SUBJECT标记并相应地填充数据库表。

Can we get the child nodes of a root node using STAX? 我们可以使用STAX获取根节点的子节点吗?

What is the best approach to do parse it: STAX or JDOM ? 解析它的最佳方法是什么: STAX还是JDOM

The XMLEventReader class in Java StAX is an Iterator based API for reading XML files. Java StAX中的XMLEventReader类是一个基于迭代器的API,用于读取XML文件。 It will let you move from event to event in the XML, allowing you to decide when to move to the next event. 它将允许您从XML中的事件移动到事件,允许您决定何时移动到下一个事件。

You are looking for "events" here. 你在这里寻找“事件”。 pls bear in mind Stax and xpath are very different things. 请记住Stax和xpath是非常不同的东西。 Stax allows you to parse a streaming XML document in a forward direction only. Stax允许您仅向前解析流式XML文档。

You can create an XMLEventReader via the javax.xml.stream.XMLInputFactory class. 您可以通过javax.xml.stream.XMLInputFactory类创建XMLEventReader。 Try running demo code ( make changes as required) and you can see the object you want. 尝试运行演示代码(根据需要进行更改),您可以看到所需的对象。

XMLInputFactory factory = XMLInputFactory.newInstance();

//get Reader connected to XML input from somewhere..
Reader reader = getXmlReader();

try {

    XMLEventReader eventReader =
    factory.createXMLEventReader(reader);

} catch (XMLStreamException e) {
    e.printStackTrace();
}

Now play around with this eventReader to get you what you want...iterate over it, see in debug mode, 现在玩这个eventReader来获得你想要的东西......迭代它,看看调试模式,

while(eventReader.hasNext()){
   // this is what you want...
    XMLEvent event = eventReader.nextEvent();

    if(event.getEventType() == XMLStreamConstants.START_ELEMENT){
        StartElement startElement = event.asStartElement();
        System.out.println(startElement.getName().getLocalPart());
    }
    //handle more event types here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM