JAXB批注 - 从XML元素提取XML值 [英] Jaxb Annotations - Extract xml value from xml element
问题描述
1)。我有一个XSD文件(我有没有控制权),我转换为使用对象模型JAXB
1.) I have an XSD file (I have no control over) that I converted to object model using JAXB
2)。我在XML格式的数据库提取物。 XML元素的标签名称是严格的表的字段名称
2.) I have a database extract in XML format. The XML element tag names are strictly the field names of the table
3)。我映射使用注释XML元素的Java类。
3.) I mapped the xml elements to the Java class using annotations.
问:是否有保持XSD文件中的元素名称,只是提取XML元素的值的方式。
Question: Is there a way to maintain the element names in the XSD file, and JUST extract the value of the xml elements.
JAXB注释类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Item", propOrder = {
"code",
"name",
"price"
})
@XmlRootElement(name="inventory")
public class Item {
@XmlElement(name="catalog_num", required = true)
protected String code;
@XmlElement(name="catalog_descrip", required = true)
protected String name;
@XmlElement(name="prod_price")
protected double price;
public String getCode() {
return code;
}
//etc
数据库xml文件的摘录:
An excerpt of the database xml file:
<?xml version="1.0"?>
<inventory>
<catalog_num>I001</catalog_num>
<catalog_descrip>Descriptive Name of Product</catalog_descrip>
<prod_price>11200</prod_price>
</inventory>
我需要编组上述xml文件后,得到的结果是:
The result I need to get after marshaling the above xml file is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Item>
<code>I001</code>
<name>Descriptive Name of Product</name>
<price>11200.0</price>
</Item>
在上面的code,我都试过了注释方法,而不是字段,但我得到了同样的结果。我只是想从XML元素中提取的值,但不能更改元素名称。
In the above code, I have tried annotating the methods instead of the fields, but I yield the same result. I just want the value extracted from the xml elements, but not change the element names.
我希望我做的感觉。
推荐答案
注意:我是的的EclipseLink JAXB(莫西)铅和JAXB的成员(的 JSR-222 )专家小组。
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
MOXY提供了一个扩展,你可以申请第二个XML通过XML文档绑定。这约束力的文件可以被用来添加元数据时,或 XML映射元数据完成=真正的
完全取代了JAXB注释的Java模型提供的元数据
MOXy offers an extension where you can apply a second XML binding via an XML document. This binding document can either be used to add metadata, or when xml-mapping-metadata-complete="true"
completely replace the metadata supplied by the JAXB annotations on the Java model;
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum6838882"
xml-mapping-metadata-complete="true">
<java-types>
<java-type name="Item" xml-accessor-type="FIELD">
<xml-root-element name="Item"/>
</java-type>
</java-types>
</xml-bindings>
该绑定文件作为创建的JAXBContext时参数传递:
The bindings file is passed as a parameter when creating the JAXBContext:
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum6838882/binding.xml");
JAXBContext resultJC = JAXBContext.newInstance(new Class[] {Item.class}, properties);
要解决您的问题,你可以创建一个JAXBContext来处理数据库文件(使用注解的类),并创建一个第二JAXBContext来处理使用MOXY绑定文件的结果格式。以下是如何做到这一点看:
To solve your issue you could create one JAXBContext to handle the database document (using the annotated classes), and create a second JAXBContext to handle the result format using the MOXy binding file. Below is how this would look:
package forum6838882;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext databaseJC = JAXBContext.newInstance(Item.class);
Unmarshaller databaseUnmarshaller = databaseJC.createUnmarshaller();
File databaseXML = new File("src/forum6838882/database.xml");
Item item = (Item) databaseUnmarshaller.unmarshal(databaseXML);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum6838882/binding.xml");
JAXBContext resultJC = JAXBContext.newInstance(new Class[] {Item.class}, properties);
Marshaller resultMarshaller = resultJC.createMarshaller();
resultMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
resultMarshaller.marshal(item, System.out);
}
}
对于更详细的例子参见:
这篇关于JAXB批注 - 从XML元素提取XML值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!