Java – Stax – takes XML nodes as strings

XML looks like this:

<statements>
   <statement account="123">
      ...stuff...
   </statement>
   <statement account="456">
      ...stuff...
   </statement>
</statements>

I was using Stax to handle a "< statement >" at that time, I got the job I need to get the entire statement node as a string so that I can create "123. XML" and "456. XML", and even load them into the database table indexed by account

Using this method: http://www.devx.com/Java/Article/30298/1954

I want to do something like this:

String statementXml = staxXmlReader.getNodeByName("statement");

//load statementXml into database

Solution

Why not use XPath directly?

You can have a fairly simple XPath to get all the 'statement' nodes

like this:

//statement

Edit #1: if possible, check Dom4j You can read the string and quite simply get all the statement nodes

Editor #2: with Dom4j, that's what you do: (from their recipes)

String text = "your xml here";
Document document = DocumentHelper.parseText(text);

public void bar(Document document) {
   List list = document.selectNodes( "//statement" );
   // loop through node data
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>