如何在java中将提取的xml元素放入新行中?

发布于 2024-12-15 17:57:58 字数 2888 浏览 0 评论 0原文

我对如何将提取的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-22 17:57:58

XQSequence 就像一个结果集 - 不要将其作为字符串获取,而是使用 next() 等来迭代它并根据需要构建字符串,使用每个单独的项目。

XQSequence is like a result set--don't get it as a string, instead use next() etc. to iterate over it and build the string as you want, using each individual item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文