如何在不创建另一个类的情况下创建内部孩子?

发布于 2024-10-12 10:03:02 字数 755 浏览 12 评论 0原文

我需要生成这样的 XML:

<Root>
   <Children>
      <InnerChildren>SomethingM</InnerChildren>
   </Children>
</Root>

最简单的解决方案是在 Root 类上创建一个内部类:

@Root
class Root{
    @Element
    Children element;

    @Root
    private static class Children{
        @Element
        String innerChildren;
    }
}

但我想避免创建内部类,因为使用 Root 对象时它会让事情看起来很奇怪。无论如何,我可以在不使用内部类的情况下实现该结果吗?

创建根对象的预期方式:

Root root = new Root("Something");

我想避免的:

Children child = new Children("Something");
Root root = new Root(child);
// this could be achieve by injecting some annotations
// in the constructor, but it's awful

I need to generate an XML like this:

<Root>
   <Children>
      <InnerChildren>SomethingM</InnerChildren>
   </Children>
</Root>

The simplest solution is creating an inner class on the Root class:

@Root
class Root{
    @Element
    Children element;

    @Root
    private static class Children{
        @Element
        String innerChildren;
    }
}

But I want to avoid that inner class creation, since it will make things look strange while using Root objects. Is there anyway I can achieve that result without using inner classes?

Expected way to create Root objects:

Root root = new Root("Something");

What I want to avoid:

Children child = new Children("Something");
Root root = new Root(child);
// this could be achieve by injecting some annotations
// in the constructor, but it's awful

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

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

发布评论

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

评论(1

剪不断理还乱 2024-10-19 10:03:02

只需使用普通类而不是内部类即可。它应该仍然有效:

@org.simpleframework.xml.Root
public class Root{
    @Element
    Children children;

    public Root(){
        children = new Children("Something");
    }
}

@org.simpleframework.xml.Root
public class Children{
    @Element
    String innerChildren;

    public Children(String inner){
        innerChildren = inner;
    }
}

更新
如果您不想创建另一个类,可以使用 Path 注释,通过为 innerChildren 字段指定 XPath 表达式来实现。例如:

@org.simpleframework.xml.Root
class Root {
   @Element
   @Path("children")
   private final String innerChildren;

   public Root(String name){
       innerChildren = name;
   }
}

生成:

<root>
   <children>
      <innerChildren>Something</innerChildren>
   </children>
</root>

使用 命名空间 注释来添加名称空间。例如:

@org.simpleframework.xml.Root
@Namespace(reference="http://domain/parent", prefix="bla")
class Root {
   @Element
   @Path("bla:children")
   @Namespace(reference="http://domain/parent", prefix="bla")
   private final String innerChildren;

   public Root(String name){
       innerChildren = name;
   }
}

Produces:

<bla:root xmlns:bla="http://domain/parent">
   <bla:children>
      <bla:innerChildren>Something</bla:innerChildren>
   </bla:children>
</bla:root>

如果使用样式来格式化 XML,则需要进行一些修改,因为它们从 Element 中删除了 :。使用样式的结果是:

<bla:root xmlns:bla="http://domain/parent">
   <blachildren>
      <bla:innerChildren>Something</bla:innerChildren>
   </blachildren>
</bla:root>

这就是我所做的:

public class MyStyle extends CamelCaseStyle{
    @Override
    public String getElement(String name) {
        if( name == null ){
            return null;
        }
        int index = name.indexOf(':');
        if( index != -1 ){
            String theRest = super.getElement(name.substring(index+1));
            return name.substring(0, index+1)+theRest;
        }
        return super.getElement(name);
    }
}

现在的结果是预期的:

<bla:Root xmlns:bla="http://domain/parent">
   <bla:Children>
      <bla:InnerChildren>Something</bla:InnerChildren>
   </bla:Children>
</bla:Root>

Just use a normal class instead of an inner class. It should still work:

@org.simpleframework.xml.Root
public class Root{
    @Element
    Children children;

    public Root(){
        children = new Children("Something");
    }
}

@org.simpleframework.xml.Root
public class Children{
    @Element
    String innerChildren;

    public Children(String inner){
        innerChildren = inner;
    }
}

Update:
If you do not want to create another class, you can use the Path annotation by specifying an XPath expression for the innerChildren field. For example:

@org.simpleframework.xml.Root
class Root {
   @Element
   @Path("children")
   private final String innerChildren;

   public Root(String name){
       innerChildren = name;
   }
}

Produces:

<root>
   <children>
      <innerChildren>Something</innerChildren>
   </children>
</root>

Use the Namespace annotation to add name spaces. For example:

@org.simpleframework.xml.Root
@Namespace(reference="http://domain/parent", prefix="bla")
class Root {
   @Element
   @Path("bla:children")
   @Namespace(reference="http://domain/parent", prefix="bla")
   private final String innerChildren;

   public Root(String name){
       innerChildren = name;
   }
}

Produces:

<bla:root xmlns:bla="http://domain/parent">
   <bla:children>
      <bla:innerChildren>Something</bla:innerChildren>
   </bla:children>
</bla:root>

If using Styles to format the XML, it's necessary to do some modifications since they remove the : from the Element. The result using styles is:

<bla:root xmlns:bla="http://domain/parent">
   <blachildren>
      <bla:innerChildren>Something</bla:innerChildren>
   </blachildren>
</bla:root>

This is what I did:

public class MyStyle extends CamelCaseStyle{
    @Override
    public String getElement(String name) {
        if( name == null ){
            return null;
        }
        int index = name.indexOf(':');
        if( index != -1 ){
            String theRest = super.getElement(name.substring(index+1));
            return name.substring(0, index+1)+theRest;
        }
        return super.getElement(name);
    }
}

And now the result is the expected one:

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