简体   繁体   English

如何创建内部子项而不创建另一个类?

[英]How to create inner children without creating another class?

I need to generate an XML like this: 我需要生成这样的XML:

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

The simplest solution is creating an inner class on the Root class: 最简单的解决方案是在Root类上创建一个内部类:

@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. 但我想避免内部类创建,因为它会在使用Root对象时使事情看起来很奇怪。 Is there anyway I can achieve that result without using inner classes? 无论如何,我可以在不使用内部类的情况下实现该结果吗?

Expected way to create Root objects: 创建Root对象的预期方法:

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

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. 更新 :如果您不想创建另一个类,可以通过为innerChildren字段指定XPath表达式来使用Path注释。 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. 使用Namespace注释添加名称空间。 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 . 如果使用样式来格式化XML,则需要进行一些修改,因为它们从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>

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

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