`
zgax
  • 浏览: 20862 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

simple-xml官方实例转载

 
阅读更多
以下转载自http://simple.sourceforge.net/download/stream/doc/examples/examples.php
This page provides a series of examples illustrating how a class can be annotated. It acts as a quick and dirty overview of how the framework can be used and also acts as a reference page. All examples can be downloaded for convenience. For further information take a look at the Tutorial.

1 Creating nested path elements
Here an example of how to use the Path annotation to create nested elements and attributes using a single annotated class. Below is an example showing two elements nested within a XPath expression.
@Root
public class Example {

   @Path("a/b[1]")
   @Element
   private String x;

   @Path("a/b[2]")
   @Element
   private String y;
}

The below snippet shows an example of the resulting XML that can be generated by this class.
<example>
  <a>
    <b>
      <x>foo</x>
    </b>
    <b>
      <y>bar</y>
    </b>
  </a>
</example>

This example can be downloaded from here.
2 Dynamically selecting an element name
Here an example of how to use the ElementUnion annotation to specify a list of options to use for serialization. The union annotation pairs an element name with a type, this allows the element name to dictate selection of the type deserialized and also allows a known type to be serialized with a specific element name.

@Root
public class Example {

   @ElementUnion({
      @Element(name="text", type=String.class),
      @Element(name="int", type=Integer.class),
      @Element(name="double", type=Double.class)
   })
   private Object value;
}

The below snippet shows an example of the resulting XML, here because the object value was an integer the resulting XML element is called int.
<example>
  <int>12</int>
</example>

This example can be downloaded from here.
3 Constructor injection
Constructor injection can be performed with any number of arguments using any of the XML annotations. In this example the Element annotation is used to identify two values to be injected in to a specific constructor.

@Root
public class Point {

   @Element
   private final int x;

   @Element
   private final int y;

   public Point(@Element(name="x") int x, @Element(name="y") int y) {
      this.x = x;
      this.y = y;
   }
}

The below snippet shows an example of the resulting XML, both the x and y values will be injected in to the annotated constructor.
<point>
  <x>10</x>
  <y>4</y>
</point>

This example can be downloaded from here.
4 Constructor injection with nested path elements
In this example constructor injection is performed on two elements which also have Path annotations. As can be seen if there is no ambiguity there is no need to specify the path annotations on the constructor. This reduces the clutter that can occur with excessive annotations.

@Root
public class Point {

   @Path("a/b[1]")
   @Element
   private final int x;

   @Path("a/b[1]")
   @Element
   private final int y;

   public Point(@Element(name="x") int x, @Element(name="y") int y) {
      this.x = x;
      this.y = y;
   }
}

The below snippet shows an example of the resulting XML, both the x and y values will be injected in to the annotated constructor.
<point>
  <a>
    <b>
      <x>2</x>
      <y>7</y>
    </b>
  </a>
</point>

This example can be downloaded from here.
5 Using namespaces
Below is an example of how to use namespaces with the Namespace annotation. Here two namespaces are declared without a prefix, this means they belong to the default namespace.
@Root
public class Example {

   @Namespace(reference="http://www.blah.com/ns/a")
   @Element
   private String a;

   @Namespace(reference="http://www.blah.com/ns/b")
   @Element
   private String b;
}

The below snippet shows an example of the resulting XML, as can be seen the namespaces are used to qualify the resulting elements.
<example>
  <a xmlns="http://www.blah.com/ns/a">foo</a>
  <b xmlns="http://www.blah.com/ns/b">bar</b>
</example>

This example can be downloaded from here.
6 Declaring a namespace prefix
When using the Namespace annotation a prefix can be specified. This prefix is added to the qualified XML elements to ensure they are within a specific namespace, rather than the default namespace.
@Root
public class Example {

   @Namespace(prefix="ns1", reference="http://www.blah.com/ns/a")
   @Element
   private String a;

   @Namespace(prefix="ns2", reference="http://www.blah.com/ns/b")
   @Element
   private String b;
}

The resulting XML shows that both elements contain the namespace prefix declared in the annotation.
<example>
  <ns1:a xmlns:ns1="http://www.blah.com/ns/a">foo</ns1:a>
  <ns2:b xmlns:ns2="http://www.blah.com/ns/b">bar</ns2:b>
</example>

This example can be downloaded from here.
7 Namespace prefix inheritance
Here a class level namespace is declared using the Namespace annotation. The element declared with the same namespace reference does not need to declare a prefix as it will be inherited from the class level annotation.
@Root
@Namespace(prefix="ns1", reference="http://www.blah.com/ns/a")
public class Example {

   @Namespace(reference="http://www.blah.com/ns/a")
   @Path("a/b")
   @Element
   private String x;
}

As can be seen in the resulting XML the namespace is declared only once, the child element inherits the original prefix reducing the verbosity of the XML.
<example xmlns:ns1="http://www.blah.com/ns/a">
  <a>
    <b>
      <ns1:x>blah</ns1:x>
    </b>
  </a>
</example>

This example can be downloaded from here.
8 Default serialization
This example shows how the Default annotation can be used. When this annotation is used fields will be serialized without the need for annotations.

@Default
public class Example {

   private List<Double> a;
   private String b;
   private String c;
   private Date d;

}

As can be seen in the resulting XML is generated for all fields within the class.
<example>
  <a>
    <double>1.2</double>
    <double>31.6</double>
    <double>52.99</double>
  </a>
  <b>foo</b>
  <c>bar</c>
  <d>2012-05-22</d>
</example>

This example can be downloaded from here.
9 Default serialization of properties
This example shows how the Default annotation can be configured to use bean methods instead of fields for serialization. When used in this manner all methods that follow the Java Bean naming conventions will be considered for serialization.
@Default(DefaultType.PROPERTY)
public class Example {

   private String name;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}


As can be seen in the resulting XML is generated for the Java Bean method.
<example>
  <name>John Doe</name>
</example>

This example can be downloaded from here.
10 Collecting various types in a single list
Here an example of how to use the ElementListUnion can be seen. This annotation allows a number of types to be declared to match a single list, all elements that match the declared names will be gathered in to the list.
@Root
public class Example {

   @ElementListUnion({
      @ElementList(entry="int", type=Integer.class, inline=true),
      @ElementList(entry="date", type=Date.class, inline=true),
      @ElementList(entry="text", type=String.class, inline=true)
   })
   private List<Object> list;
}

The below snippet shows an example of the resulting XML, each type is given a name according to its type.
<example>
  <int>12</int>
  <date>2012-22-05</date>
  <date>1977-18-11</date>
  <text>blah</text>
  <int>1</int>
  <int>34525</int>
  <date>2001-01-05</date>
</example>

This example can be downloaded from here.
11 Dynamically matching a constructor
Here an example of how to use the ElementUnion to dynamically select a constructor based on the value deserialized. Constructor matching will be done by examining the declared name and the instance type.
@Root
public class Example {

   @ElementUnion({
      @Element(name="int", type=Integer.class),
      @Element(name="date", type=Date.class),
      @Element(name="text", type=String.class)
   })
   private Object value;

   public Example(@Element(name="int") int value) {
      this.value = value;
   }

   public Example(@Element(name="date") Date value) {
      this.value = value;
   }

   public Example(@Element(name="text") String value) {
      this.value = value;
   }
}

The below snippet shows an example of the resulting XML, here the constructor accepting a date will be invoked as that is what is deserialized from the file.
<example>
  <date>2001-01-05</date>
</example>

This example can be downloaded from here.
分享到:
评论

相关推荐

    simple-xml-2.7.1

    只需提供XML字符串或输入流,`Persister`类就能创建相应的Java实例: ```java Person person = serializer.read(Person.class, xml); ``` 3. **性能优势**:Simple-XML通过反射和缓存策略,优化了序列化和反序列化...

    VTD-XML技术解析XML实例

    然而,XML文档的大小和复杂性可能导致传统DOM(Document Object Model)和SAX(Simple API for XML)解析器在处理时遇到性能问题。VTD-XML通过创建VTD索引来解决这个问题,它只需要加载部分或关键部分的XML数据到...

    XML轻松学习手册--XML实例解析之一

    《XML轻松学习手册--XML实例解析之一》这篇文章主要介绍了如何使用XML实现一个简单的CD唱片数据检索功能,让我们深入了解XML在Web应用中的数据操作能力。XML(eXtensible Markup Language)是一种可扩展标记语言,...

    XML-Simple-2.20

    XML-Simple是Perl编程语言中一个非常流行的模块,主要用于解析和生成XML文档。这个压缩包“XML-Simple-2.20”包含了该模块的2.20版本,这是一个用于简化处理XML数据的库。Perl开发者经常使用XML-Simple来处理XML数据...

    未来战士--XML网络编程实例

    解析器分为两种类型:DOM(Document Object Model)解析器和SAX(Simple API for XML)解析器。DOM解析器一次性加载整个XML文档到内存,形成一棵树形结构,便于随机访问任何节点;而SAX解析器采用事件驱动的方式,...

    PHP经典实例--XML

    **PHP经典实例--XML** XML(Extensible Markup Language)是一种用于标记数据的语言,它具有自我描述性和灵活性,常用于在不同系统间交换数据。在PHP中,XML处理是开发Web服务、API接口以及数据存储的关键技术。本...

    dubbo-monitor-simple-2.5.5 最新安装包

    3. 扩展性:随着服务规模的扩大,可能需要部署多个Monitor实例,通过负载均衡来分摊压力。 总的来说,"Dubbo-monitor-simple-2.5.5"是Dubbo监控功能的重要组成部分,它的易用性和稳定性为开发者提供了强大的服务...

    VDT-XML解析xml文件,修改、查找xml文件。

    - 解析过程:VDT-XML首先读取XML文件,然后创建一个解析器实例,该实例可以遍历XML树并访问各个节点。 - 流式解析:如果内存有限,可以使用流式解析,只加载当前处理的部分,而不是一次性加载整个XML文档。 3. **...

    dubbo-monitor-simple-2.5.10-assembly.tar.gz

    `dubbo-monitor-simple`是Dubbo官方提供的一个基础监控实现,它基于Servlet实现,提供了基本的监控接口,如服务调用统计、服务接口的QPS(每秒请求数)、TP99(99%响应时间)等关键指标。 2. **核心组件** - `...

    Android开发-Retrofit-AndroidStudio(四)simplexml解析XML_免积分

    简单XML库(Simple XML)是Java中一个轻量级、高效的XML序列化和反序列化库,非常适合处理XML格式的数据。 首先,让我们了解Retrofit的基本概念。Retrofit由Square公司开发,它允许开发者通过简单的注解将HTTP操作...

    simple-demo.rar

    "simple-demo"这个名字暗示这是一个简单的演示或教程,可能是某个软件、框架或编程语言的应用实例。 在解压"simple-demo.rar"后,我们发现只有一个同名文件"simple-demo"。这可能是一个单一的文件,如一个可执行...

    dubbo-demo-consumer、dubbo-demo-provider、dubbo-simple-monitor

    本篇将详细讲解基于dubbo-demo-consumer、dubbo-demo-provider和dubbo-simple-monitor的实例服务,带你深入理解Dubbo的核心概念和操作流程。 首先,我们来看`dubbo-demo-consumer`,它是Dubbo服务的消费者。消费者...

    XML基础教程实例代码

    - 在提供的"XML实例代码"中,你可能会找到创建基本XML文档、使用命名空间、应用XML Schema验证、使用XPath选取节点以及XSLT转换的例子。 学习这些基础知识,配合实际的代码示例,将有助于你快速掌握XML。实践是...

    simple-rpc是一款基于netty的RPC框架

    3. **Spring集成**:`Simple-RPC`支持与Spring框架的无缝集成,可以通过XML配置或者Java Config方式将RPC服务注册到Spring容器中,便于依赖注入和管理。 **XML配置和Java Config** - **XML配置**:遵循Spring的配置...

    dubbo-monitor-simple-2.5.3.zip dubbo的监控中心,亲测可用使用

    4. 横向扩展:在高并发场景下,可考虑部署多个监控实例,提高监控系统的可用性和稳定性。 总结,Dubbo Monitor Simple 2.5.3作为Dubbo生态中的重要一环,为开发者提供了直观、易用的服务监控能力。掌握其安装、配置...

    xml网页实例

    - XML解析器:用于读取和处理XML文件,如DOM(Document Object Model)和SAX(Simple API for XML)。 - XSLT处理器:如Saxon,用于执行XSLT转换。 通过这个“xml网页实例”,你可以实践XML的基本概念,理解如何...

    CPP-XML.rar_xml

    书中可能会有实例代码展示如何创建XML文档,如何查找和修改特定元素,如何处理XML命名空间,以及如何使用XPath或XSLT来查询和转换XML数据。 此外,可能还会涉及错误处理、性能优化和内存管理等方面的内容,这些都是...

    simple-spring-memcached统一缓存的使用实例[整理].pdf

    《Simple-Spring-Memcached统一缓存的使用详解》 在Java应用中,尤其是在中大型项目中,有效地管理和使用缓存对于提升系统性能至关重要。Simple-Spring-Memcached(SSM)是一个流行的缓存框架,它整合了Spring框架...

    基于Java的实例源码-XML解析器 Xerces.zip

    Xerces-Java是Xerces解析器的Java实现,提供了多种XML解析技术,包括DOM(Document Object Model)、SAX(Simple API for XML)和JAXP(Java API for XML Processing)。这些解析方式各有特点: 1. DOM:这是一种将...

    报文日志文件入库实例---XML解析

    2. **SAX解析器**:SAX(Simple API for XML)是一种事件驱动的解析方式,它不需要一次性加载整个文档,适合处理大型XML文件。Java中的`org.xml.sax.helpers.DefaultHandler`和`org.xml.sax.XMLReader`类是实现SAX...

Global site tag (gtag.js) - Google Analytics