SimpleFramework XML:具有内部文本和子元素的元素

时间:2012-11-16 12:55:15

标签: java simple-framework

我有这种格式的XML:

<element1>
     some inner text
     <child1>11</child1>
     <child2>True</child2>
</element1>

我尝试通过简单的框架将这个XML映射到对象上:

@Element(name = "element1")
public static class Something {

    @Element(required=false)
    public int child1;

    @Element(required=false)
    public boolean child2;

}

我无法更改XML,我需要在类中的某些字段上映射值“some inner text”。不幸的是,我不知道该怎么做(如果可能的话)。我试过@Text注释,但它不能和@Element一起用在一个类中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:6)

编辑:您实际上可以使用2.6.9及更高版本执行此操作。请使用以下注释,请注意@Text注释可与@ElementListUnion一起使用。

@Root
public static class Something {

    @Text 
    @ElementListUnion({
       @ElementList(entry="child1", type=Integer.class, inline=true),
       @ElementList(entry="child2", type=Boolean.class, inline=true)
    })
    public List<Object> values;
}

这只能作为黑客作业完成,默认情况下,简单不支持自由文本,因为它不会一致地映射到对象。以下是如何做到这一点,但它可能不值得,除非这是一个在Simple文件中运行良好的大文件中的特殊情况。

package org.simpleframework.xml.convert;

import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import org.simpleframework.xml.Root;
import org.simpleframework.xml.ValidationTestCase;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.NodeBuilder;
import org.simpleframework.xml.stream.OutputNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class HackJobToGrabFloatingTextTest extends ValidationTestCase {

   private static final String SOURCE =
   "<element1>\n"+
   "   some inner text\n"+
   "   <child1>11</child1>\n"+
   "   <child2>True</child2>\n" +
   "</element1>";

   private static class SomethingConverter implements Converter<Something> {

      public Something read(InputNode node) throws Exception {
         Object source = node.getSource();
         Element element = (Element)source;
         NodeList elements = element.getChildNodes();
         String child1 = null;
         String child2 = null;
         String text = "";
         for(int i = 0; i < elements.getLength(); i++) {
            Node next = elements.item(i);
            if(next.getNodeType() == Node.TEXT_NODE) {
               text += next.getNodeValue();
            }
            if(next.getNodeType() == Node.ELEMENT_NODE) {
               if(next.getNodeName().equals("child1")) {
                  child1 = next.getTextContent();
               }
               if(next.getNodeName().equals("child2")) {
                  child2 = next.getTextContent();
               }
            }
         }
         return new Something(child1, child2, text.trim());
      }

      public void write(OutputNode node, Something car) throws Exception {
         // Free text not supported!!
      }
   }

   @Root(name = "element1")
   @Convert(SomethingConverter.class)
   public static class Something {
       public String child1;
       public String child2;
       public String text;
       public Something(String child1, String child2, String text) {
          this.child1 = child1;
          this.child2 = child2;
          this.text = text;
       }
   }

   public void testHackJob() throws Exception {
      Class<?> type = Class.forName("org.simpleframework.xml.stream.DocumentProvider");
      Constructor<?> constructor = type.getDeclaredConstructor();
      constructor.setAccessible(true);
      Object value = constructor.newInstance();
      Field[] fields = NodeBuilder.class.getDeclaredFields();

      for(Field field : fields) {
         if(field.getName().equalsIgnoreCase("provider")) {
            field.setAccessible(true);
            field.set(null, value);
         }
      }
      StringReader reader = new StringReader(SOURCE);
      InputNode source = NodeBuilder.read(reader);
      AnnotationStrategy strategy = new AnnotationStrategy();
      Persister persister = new Persister(strategy);
      Something something = persister.read(Something.class, source);

      assertNotNull(something);
      assertEquals(something.text, "some inner text");
      assertEquals(something.child1, "11");
      assertEquals(something.child2, "True");
   }
}