我遇到了如何将提取的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,但似乎根本不起作用.. 我的问题的任何解决方案?谢谢!
答案 0 :(得分:0)
XQSequence
就像一个结果集 - 不要把它作为一个字符串,而是使用next()
等来迭代它并使用每个单独的项目构建所需的字符串。