stax-以字符串形式获取xml节点 [英] stax - get xml node as string
问题描述
xml看起来像这样:
xml looks like so:
<statements>
<statement account="123">
...stuff...
</statement>
<statement account="456">
...stuff...
</statement>
</statements>
我正在使用stax一次处理一个"<statement>
",并且可以正常工作.我需要将整个语句节点作为字符串获取,以便可以创建"123.xml"和"456.xml",甚至可以将其加载到按帐户索引的数据库表中.
I'm using stax to process one "<statement>
" at a time and I got that working. I need to get that entire statement node as a string so I can create "123.xml" and "456.xml" or maybe even load it into a database table indexed by account.
使用这种方法: http://www.devx.com/Java/Article/30298/1954
我正在做这样的事情:
String statementXml = staxXmlReader.getNodeByName("statement");
//load statementXml into database
推荐答案
为什么不仅仅为此使用xpath?
Why not just use xpath for this?
您可以使用一个相当简单的xpath来获取所有语句"节点.
You could have a fairly simple xpath to get all 'statement' nodes.
像这样:
//statement
编辑#1:如果可能,请查看 dom4j .您可以读取String并相当简单地获取所有"statement"节点.
EDIT #1: If possible, take a look at dom4j. You could read the String and get all 'statement' nodes fairly simply.
编辑#2:使用dom4j,这是您要执行的操作: (摘自他们的食谱)
EDIT #2: Using dom4j, this is how you would do it: (from their cookbook)
String text = "your xml here";
Document document = DocumentHelper.parseText(text);
public void bar(Document document) {
List list = document.selectNodes( "//statement" );
// loop through node data
}
这篇关于stax-以字符串形式获取xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!