XmlPath语法
2012-05-22 09:45
218 查看
节点的关系:
父(Parent)
子(Children)
同胞(Sibling)
先辈(Ancestor)
后代(Descendant)
路径表达式
nodename 选取此节点的所有子节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
. 选取当前节点
.. 选取当前节点的父节点
@ 选取属性
谓语(Predicates)
谓语被嵌在方括号中。
/bookstore/book[1]
/bookstore/book[last()]
/bookstore/book[price<10]
通配符
* 匹配任何元素节点
@* 匹配任何属性节点
node() 匹配任何类型的节点
XPath 轴
ancestor 选取当前节点的所有先辈(父、祖父等)
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
attribute 选取当前节点的所有属性
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
namespace 选取当前节点的所有命名空间节点
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。
摘自:http://hi.baidu.com/dming4/blog/item/7f18d1ef1451fde5ce1b3ecf.html
摘自官网:http://www.eclipse.org/eclipselink/api/2.3/org/eclipse/persistence/oxm/annotations/XmlPath.html
父(Parent)
子(Children)
同胞(Sibling)
先辈(Ancestor)
后代(Descendant)
路径表达式
nodename 选取此节点的所有子节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
. 选取当前节点
.. 选取当前节点的父节点
@ 选取属性
谓语(Predicates)
谓语被嵌在方括号中。
/bookstore/book[1]
/bookstore/book[last()]
/bookstore/book[price<10]
通配符
* 匹配任何元素节点
@* 匹配任何属性节点
node() 匹配任何类型的节点
XPath 轴
ancestor 选取当前节点的所有先辈(父、祖父等)
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
attribute 选取当前节点的所有属性
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
namespace 选取当前节点的所有命名空间节点
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。
摘自:http://hi.baidu.com/dming4/blog/item/7f18d1ef1451fde5ce1b3ecf.html
@Target(value={FIELD,METHOD}) @Retention(value=RUNTIME) public @interface XmlPath XPath based mapping is what allows an existing object model to be mapped to an existing XML schema. The @XmlPathannotation is the means by which XPath based mapping is achieved. Example 1 - Using @XmlPathto Add a Grouping Element Sometimes grouping elements are added to your document to organise data. JAXB has this concept for collection properties in the form of @XmlElementWrapper. Here we'll use @XmlPathfor non-collection properties. In this case we'll nest the billing/shipping address data within the "contact-info" element. import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlPath("contact-info/billing-address") private Address billingAddress; @XmlPath("contact-info/shipping-address") private Address shippingAddress; } This will produce XML like: <customer> <contact-info> <billing-address> <street>1 Billing Street</street> </billing-address> <shipping-address> <street>2 Shipping Road</street> </shipping-address> </contact-info> </customer> Example 2 - Using @XmlPathto Map by Position Normally in JAXB elements with the same name must be mapped to a collection property. Using the @XmlPath extension you map non-collection properties to a repeated element by index. import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlPath("address[1]") private Address billingAddress; @XmlPath("address[2]") private Address shippingAddress; } This will produce XML like: <customer> <address> <street>1 Billing Street</street> </address> <address> <street>2 Shipping Road</street> </address> </customer> Example 3 - Using @XmlPathto Map Two Objects to the Same Node We have seen how @XmlPathcan be used to expand the structure by adding a grouping element. @XmlPathcan also be used to collapse the structure by mapping two objects to the same node. import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlPath(".") private Address billingAddress; private Address shippingAddress; } This will produce XML like: <customer> <street>1 Billing Street</street> <shippingAddress> <street>2 Shipping Road</street> </shippingAddress> </customer>
valuepublic abstractjava.lang.String value The XPath for this property. A subset of the XPath specification may be used to specify mappings. The following concepts are supported: Attribute - "@id" Element - "address" Element by Position - "address[1]" Element by Predicate - "address[@type='mailing']" Element Text - "name/text()" Text - "text()" Self - "." Combination - "personal-info/name[2]/text()" For namespace qualified nodes, the prefixes defined in the XmlNs annotations can be used to qualify the XPath fragments. Unqualified fragments will assumed to be in the namespace specified using @XmlSchema. Example Assuming the following namespace information has been set up using the @XmlSchema annotation: @XmlSchema(namespace = "http://www.example.org/FOO", xmlns = {@XmlNs(prefix="ns", namespaceURI="http://www.example.com/BAR")}, elementFormDefault = XmlNsForm.QUALIFIED) package org.example; import javax.xml.bind.annotation.*; Then the following XPath: @XmlPath("contact-info/ns:address/@id") Will be qualified as: contact-info - in "http://www.example.org/FOO" namespace. address - in "http://www.example.com/BAR" namespace. @id - in no namespace. See Also: XmlSchema, XmlNs |
相关文章推荐
- XMLPath \ XML 路径 语法
- XMLPath \ XML 路径 语法
- XmlPath语法
- XML轻松学习手册(4)XML语法-续2
- javaWeb-xml语法详解
- XML基础知识学习三(XML 语法)
- Non-readable POM %path%pom.xml
- XML(二)——语法简介
- ClassPathXmlApplicationContext()寻找文件路径
- 关于ClassPathXmlApplicationContext以及FileSystemXmlApplicationContext的问题
- 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- IDEA中Spring配置错误:class path resource [.xml] cannot be opened because it does not exist
- sql server 使用for xml path 将1列多行转换为字符串连接起来
- XML基本语法
- 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- 使用For XML PATH 会影响Cross Apply 返回
- XML的基本语法(XML实现html的网页布局的简单功能)
- SQL Server FOR XML PATH 语句的应用
- php中xml语法解析总结
- 合并多行查询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数