如何在java中将提取的xml元素放入新行中?
我对如何将提取的 xml 元素放入新行有疑问?因为我得到的是元素显示在一行中。
这是 xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<PayrollRequest>
<PayrollCost>
<PayrollID>123</PayrollID>
<BatchID>
<No>7770</No>
<No>8888</No>
<No>1234</No>
<No>9999</No>
</BatchID>
<CompanyId>
<CompanyId>001</CompanyId>
<CompanyDescription>KukuKambing</CompanyDescription>
</CompanyId>
<CategoryCost>
<GrossPay>60000</GrossPay>
</CategoryCost>
</PayrollCost>
</PayrollRequest>
这是java中的代码:
import javax.xml.namespace.QName;
import java.util.Properties;
import com.ddtek.xquery3.XQConnection;
import com.ddtek.xquery3.XQException;
import com.ddtek.xquery3.XQExpression;
import com.ddtek.xquery3.XQItemType;
import com.ddtek.xquery3.XQSequence;
import com.ddtek.xquery3.xqj.DDXQDataSource;
public class XQueryTest4 {
// Filename for XML document to query
private String filename;
// Data Source for querying
private DDXQDataSource dataSource;
// Connection for querying
private XQConnection conn;
public XQueryTest4(String filename) {
this.filename = "SamplePayroll2.xml";
}
public void init() throws XQException {
dataSource = new DDXQDataSource();
conn = dataSource.getConnection();
}
public String query(String queryString) throws XQException {
XQExpression expression = conn.createExpression();
expression.bindString(new QName("docName"), filename,
conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
XQSequence results = expression.executeQuery(queryString);
return results.getSequenceAsString(new Properties());
}
public static void main(String[] args) {
try {
XQueryTest4 tester = new XQueryTest4("SamplePayroll2.xml");
tester.init();
final String sep = System.getProperty("line.separator");
String queryString =
"declare variable $docName as xs:string external;" + sep +
" for $PayrollRequest in doc($docName)/PayrollRequest " +
" return " +
"$PayrollRequest/PayrollCost/BatchID/No/text()";
System.out.println(tester.query(queryString));
} catch (Exception e) {
e.printStackTrace(System.err);
System.err.println(e.getMessage());
}
}
}
我得到的输出在一行中:7770888812349999
但我想要的是,它应该是这样的:
7770
8888
1234
9999
我尝试使用\n\r,但似乎不是'根本不工作.. 我的问题有什么解决办法吗?谢谢!
im having a problem on how to put the extracted xml elements in a new line? because what i get is the elements are displayed in one single line.
this is the xml file.
<?xml version="1.0" encoding="UTF-8"?>
<PayrollRequest>
<PayrollCost>
<PayrollID>123</PayrollID>
<BatchID>
<No>7770</No>
<No>8888</No>
<No>1234</No>
<No>9999</No>
</BatchID>
<CompanyId>
<CompanyId>001</CompanyId>
<CompanyDescription>KukuKambing</CompanyDescription>
</CompanyId>
<CategoryCost>
<GrossPay>60000</GrossPay>
</CategoryCost>
</PayrollCost>
</PayrollRequest>
and this is the code in java:
import javax.xml.namespace.QName;
import java.util.Properties;
import com.ddtek.xquery3.XQConnection;
import com.ddtek.xquery3.XQException;
import com.ddtek.xquery3.XQExpression;
import com.ddtek.xquery3.XQItemType;
import com.ddtek.xquery3.XQSequence;
import com.ddtek.xquery3.xqj.DDXQDataSource;
public class XQueryTest4 {
// Filename for XML document to query
private String filename;
// Data Source for querying
private DDXQDataSource dataSource;
// Connection for querying
private XQConnection conn;
public XQueryTest4(String filename) {
this.filename = "SamplePayroll2.xml";
}
public void init() throws XQException {
dataSource = new DDXQDataSource();
conn = dataSource.getConnection();
}
public String query(String queryString) throws XQException {
XQExpression expression = conn.createExpression();
expression.bindString(new QName("docName"), filename,
conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
XQSequence results = expression.executeQuery(queryString);
return results.getSequenceAsString(new Properties());
}
public static void main(String[] args) {
try {
XQueryTest4 tester = new XQueryTest4("SamplePayroll2.xml");
tester.init();
final String sep = System.getProperty("line.separator");
String queryString =
"declare variable $docName as xs:string external;" + sep +
" for $PayrollRequest in doc($docName)/PayrollRequest " +
" return " +
"$PayrollRequest/PayrollCost/BatchID/No/text()";
System.out.println(tester.query(queryString));
} catch (Exception e) {
e.printStackTrace(System.err);
System.err.println(e.getMessage());
}
}
}
the output that i get is in one single line: 7770888812349999
but what i want is, it should be like this:
7770
8888
1234
9999
i have tried to use the \n\r, but is seems to be doesn't work at all..
any solutions for my problems? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XQSequence
就像一个结果集 - 不要将其作为字符串获取,而是使用next()
等来迭代它并根据需要构建字符串,使用每个单独的项目。XQSequence
is like a result set--don't get it as a string, instead usenext()
etc. to iterate over it and build the string as you want, using each individual item.