简体   繁体   English

在Java中创建动态XML

[英]Creating dynamic xml in java

I'm new to creating xmls.I'm able to create small xml but,I need to create dynamic xml based on the information send from the user ,required xml format is 我是创建xml的新手。我可以创建小型xml,但是,我需要根据用户发送的信息创建动态xml,所需的xml格式为

can any one help me to create the above xml. 谁能帮我创建上面的xml。

If the only thing required is XML format, you can choose NanoXML , which is very light and has simple API. 如果仅需要XML格式,则可以选择NanoXML ,它非常轻便并且具有简单的API。

If you want to change Java objects into XML, your best choice may be XStream , in which object-to-XML is single line instruction. 如果要将Java对象更改为XML,最好的选择是XStream ,其中对象到XML是单行指令。

Do you want to create String representations of XML only? 您是否只想创建XML的字符串表示形式? Then by far the most pragmatic way to do so is by simply using Strings. 那么到目前为止,最实用的方法是仅使用Strings。 For example: 例如:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String asXML() {
        return String.format("<person><name>%s</name></person>", name);
    }
}

You can compose graphs of objects in this way, call asXML on the top level object and get an entire document. 您可以通过这种方式构成对象图,在顶级对象上调用asXML并获得整个文档。

I'd advise you to try StAX . 我建议您尝试StAX It's part of standard java api, and it's quite simple to use. 它是标准Java API的一部分,使用起来非常简单。

Here is great tutorial to start with StAX 这是从StAX开始的出色教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM