从 java List 对象创建 XML 的最有效方法

发布于 2024-11-25 20:09:52 字数 1981 浏览 4 评论 0 原文

我正在努力将 CSV 转换为 XML,尽管我有很多方法可以做到这一点,但我的要求是这样做,以便将来可以在不更改任何代码的情况下更改映射。 所以我们使用以下方法。 我们使用 Apache Camel 进行集成,因此 CSV 通过 Camel 进行开箱即用的转换,并以 List> 的形式提供 对于数据,

{[header1,header2,header3],[1,2,3],[2,4,5]}

我需要将此列表数据转换为 XML 是定义的形式,但由于我无法使用 java 映射,所以计划做类似的事情 使用 XStream 将此列表转换为平面 XML 文件,然后使用 xslt 将原始 XML 文件映射到 xslt,但是当我使用 XStream 将列表转换为 XML 时,它会给出以下输出,

<list>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>Quantity</string>
      <string>Price</string>
      <string>Total</string>
      <string>Date</string>
      <string>ID</string>
      <string>Name</string>
      <string>Ref#</string>
    </a>
  </java.util.Arrays_-ArrayList>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>4</string>
      <string>1.13</string>
      <string>4.52</string>
      <string>9/4/2008</string>
      <string>275</string>
      <string>Blue Ink</string>
      <string>49385730</string>
    </a>
  </java.util.Arrays_-ArrayList>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>5</string>
      <string>2.16</string>
      <string>2.16</string>
      <string>8/3/2008</string>
      <string>229</string>
      <string>Red Ink</string>
      <string>20549348</string>
    </a>

我的示例 CSV 是

Quantity,Price,Total,Date,ID,Name,Ref#
4,1.13,4.52,9/4/2008,275,Blue Ink,49385730
5,2.16,2.16,8/3/2008,229,Red Ink,20549348

因此似乎不可能使用 XSLT 将这个原始 XML 映射到所需的 XML。 有没有办法将此列表数据转换为某种更美观的 XML 格式,以便我们可以使用 XSLT 映射该 XML 以避免任何代码更改。 或者是否有其他更有效的方法将此列表转换为 XML 结构

提前致谢

i am working on converting CSV to XML, though i have many ways to do this but my requirement is to do it in such a way that in future the mapping can be changed without any code change.
so we are using the following approach.
we are using Apache camel for the integration so CSV is being converted out of the box by camel and is being provided as a List<List<String>>
with data something like

{[header1,header2,header3],[1,2,3],[2,4,5]}

i need to convert this list data to XML is the defined form but since i can't use the java mapping so was planning to do something like
to convert this List in to a flat XML file using XStream and than use xslt to map the raw XML file to xslt, but when i am XStream to convert List to XML it giving following output

<list>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>Quantity</string>
      <string>Price</string>
      <string>Total</string>
      <string>Date</string>
      <string>ID</string>
      <string>Name</string>
      <string>Ref#</string>
    </a>
  </java.util.Arrays_-ArrayList>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>4</string>
      <string>1.13</string>
      <string>4.52</string>
      <string>9/4/2008</string>
      <string>275</string>
      <string>Blue Ink</string>
      <string>49385730</string>
    </a>
  </java.util.Arrays_-ArrayList>
  <java.util.Arrays_-ArrayList>
    <a class="string-array">
      <string>5</string>
      <string>2.16</string>
      <string>2.16</string>
      <string>8/3/2008</string>
      <string>229</string>
      <string>Red Ink</string>
      <string>20549348</string>
    </a>

my sample CSV is

Quantity,Price,Total,Date,ID,Name,Ref#
4,1.13,4.52,9/4/2008,275,Blue Ink,49385730
5,2.16,2.16,8/3/2008,229,Red Ink,20549348

so it seems not possible to map this raw XML to the required XML using XSLT.
Is there any way to convert this List data in to some more presentable XML format so that we can map that XML with XSLT to avoid any code change.
Or is there any other more efficient way to convert this list in to XML structure

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

违心° 2024-12-02 20:09:52

生成的 XML 的描述方式(标头成为元素标签),如果没有自定义编程,确实很难生成。最好的选择是使用 DOM4J 自己编写一些代码。

The way how the generated XML is described (headers become the element tags), it is really hard to generate without custom programming. The best bet would be to write some code yourself using DOM4J.

黑白记忆 2024-12-02 20:09:52

这比我想象的更复杂,主要是由于列表输入:

    final List<String> headers = Arrays.asList("Quantity", "Price");
    final List<String> row1 = Arrays.asList("1", "2");
    final List<String> row2 = Arrays.asList("3", "4");
    final XStream xStream = new XStream();
    xStream.autodetectAnnotations(true);
    final String xml = xStream.toXML(new Container(new Headers(headers), Arrays.asList( row1, row2)));

这将为您提供:

<Container>
  <Headers>
    <header>Quantity</header>
    <header>Price</header>
  </Headers>
  <Part>
    <Item>1</Item>
    <Item>2</Item>
  </Part>
  <Part>
    <Item>3</Item>
    <Item>4</Item>
  </Part>
</Container>

容器:

@XStreamAlias("Container")
class Container {
    @XStreamAlias("Headers")
    private final Headers headers;

    @XStreamImplicit(itemFieldName = "Part")
    private final List<LineItem> items = new ArrayList<LineItem>();

    Container(final Headers headers, final List<List<String>> stringItems) {
        this.headers = headers;
        for (List<String> item : stringItems) {
            items.add(new LineItem(item));
        }
    }
}

标题:

@XStreamAlias("Headers")
public class Headers {
    @XStreamImplicit(itemFieldName = "header")
    private final List<String> headers;

    public Headers(final List<String> headers) {
        this.headers = headers;
    }
}

和行项目:

@XStreamAlias("Items")
public class LineItem {
    @XStreamImplicit(itemFieldName = "Item" )
    private final List<String> items;

    public LineItem(final List<String> items) {
        this.items = items;
    }
}

This was more complex than I thought, mostly due to the List input:

    final List<String> headers = Arrays.asList("Quantity", "Price");
    final List<String> row1 = Arrays.asList("1", "2");
    final List<String> row2 = Arrays.asList("3", "4");
    final XStream xStream = new XStream();
    xStream.autodetectAnnotations(true);
    final String xml = xStream.toXML(new Container(new Headers(headers), Arrays.asList( row1, row2)));

This will give you:

<Container>
  <Headers>
    <header>Quantity</header>
    <header>Price</header>
  </Headers>
  <Part>
    <Item>1</Item>
    <Item>2</Item>
  </Part>
  <Part>
    <Item>3</Item>
    <Item>4</Item>
  </Part>
</Container>

Container:

@XStreamAlias("Container")
class Container {
    @XStreamAlias("Headers")
    private final Headers headers;

    @XStreamImplicit(itemFieldName = "Part")
    private final List<LineItem> items = new ArrayList<LineItem>();

    Container(final Headers headers, final List<List<String>> stringItems) {
        this.headers = headers;
        for (List<String> item : stringItems) {
            items.add(new LineItem(item));
        }
    }
}

Headers:

@XStreamAlias("Headers")
public class Headers {
    @XStreamImplicit(itemFieldName = "header")
    private final List<String> headers;

    public Headers(final List<String> headers) {
        this.headers = headers;
    }
}

and LineItems:

@XStreamAlias("Items")
public class LineItem {
    @XStreamImplicit(itemFieldName = "Item" )
    private final List<String> items;

    public LineItem(final List<String> items) {
        this.items = items;
    }
}
攒眉千度 2024-12-02 20:09:52

我认为 XSLT 可以完成这项工作:

您可以使用 xslt 的 position() 函数来当您迭代所有列表条目时,导航到第一个列表条目中的右侧列标识符。

I think XSLT can do the job:

You can use the position() function of xslt to navigate to the right column identifier within the first list entry when you iterate over all the list entries.

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