首页 > 解决方案 > Aspose DataSet.readXml 产生更复杂的结构

问题描述

所以我正在检查 Java 的示例,并注意到使用InTableList.doc/InTableList来自https://github.com/aspose-words/Aspose.Words-for-Java/tree/master/Examples/src的示例。

假设代码

他们在 Common.java 文件中手动创建数据集。代码的小子摘录:

// Create a new data set
        DataSet ds = new DataSet("ds");

        // Add a new table to store contracts
        DataTable dtContracts = new DataTable("Contracts");

        // Add a new table to store managers
        DataTable dtManagers = new DataTable("Managers");

        // Add a new table to store clients
        DataTable dtClients = new DataTable("Clients");

        // Add columns to Managers table
        dtManagers.getColumns().add("Id", int.class);
        dtManagers.getColumns().add("Name");
        dtManagers.getColumns().add("Age", int.class);
        dtManagers.getColumns().add("Photo", byte[].class);
        ds.getTables().add(dtManagers);

        // Add columns to Contracts table
        dtContracts.getColumns().add("Id", int.class);
        dtContracts.getColumns().add("ClientId", int.class);
        dtContracts.getColumns().add("ManagerId", int.class);
        dtContracts.getColumns().add("Price", float.class);
        dtContracts.getColumns().add("Date", Date.class);
        ds.getTables().add(dtContracts);

        // Add columns to Clients table
        dtClients.getColumns().add("Id", int.class);
        dtClients.getColumns().add("Name");
        ds.getTables().add(dtClients);
        ds.getRelations().add(dtClients,dtContracts, "Id","ClientId");
        ds.getRelations().add(dtManagers,dtContracts, "Id","ManagerId");

而word模板是这样的:

<<foreach [m in ds.Managers]>><<[m.Name]>>  <<[m.Contracts.sum(
c => c.Price)]>><</foreach>>

Total:  <<[ds.Contracts.
sum(
c => c.Price)]>>

这看起来很简单,不需要根节点。

我的代码

我有以下 XML 结构:

<root>
    <reference>b</reference>
    <caseHandler>
        <fullName>dddd</fullName>
        <active>true</active>
    </caseHandler>
    <name>This is the name of the case</name>
    <deadline>1528220937444</deadline>
    <associated>
        <code>ddd</code>
        <active>true</active>
        <id>-9</id>
        <parentScopeId>-3</parentScopeId>
        <title>ddddddddddd</title>
    </associated>
    <associated>
        <code>ddd</code>
        <active>true</active>
        <id>-9</id>
        <parentScopeId>-3</parentScopeId>
        <title>ddddddddddd</title>
    </associated>
</root>

我正在将 XML 加载到数据集中,如下所示:

DataSet set = new DataSet("ds");
set.readXml(new ByteArrayInputStream(escaped.getBytes()));

log.info("XML " + otherXml);

ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, set);//model

问题

基础报告坚持所有内容都在“ROOT”节点下。显然,这是<root>xml 标签。如何修改我的设置以摆脱这个额外的级别?

我的问题是我需要用与此“根”相关的“foreach”将单词文件中的所有文本包围起来。

<<foreach [c in root]>> 

<< [c.name] >> << [c.reference] >>

Case Handler : 
<< [c.caseHandler.first().fullName] >>


<<foreach [in c.associated]>>
    (<<[code]>>)    <<[title]>>                     <</foreach>>


 <</foreach>>

这很烦人……

我肯定更喜欢像这样更简单的东西:

 << [ds.name] >> << [ds.reference] >>

    Case Handler : 
    << [ds.caseHandler.first().fullName] >>


    <<foreach [in ds.associated]>>
        (<<[code]>>)    <<[title]>>                     <</foreach>>

问题

如何简化结构以便我仍然能够读取 XML,但我不需要 word 文件中的额外开销?

标签: javaxmldatasetaspose.words

解决方案


请使用如下所示的 ReportingEngine.buildReport 来获得所需的输出。

模板:<<[ds.name]>> <<[ds.reference]>>

DataSet set = new DataSet("ds");
set.readXml(MyDir + "in.xml");

Document doc = new Document(MyDir + "template.docx");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, set.getTables().get("root").getRows().get(0), "ds");//model

doc.save(MyDir + "output.docx");

我与 Aspose 一起担任开发人员宣传员。


推荐阅读