使用MOXy和XPath,是否可以解组属性列表? [英] With MOXy and XPath, is it possible to unmarshal a list of attributes?
问题描述
编辑:这是我加载XML文档的方式,就像我在Blaise的回答中使用的那样。我正在加载它,因为我想使用节点,而不是整个文档。即使使用整个文档,我在以这种方式加载时仍然遇到问题。
here's how I'm loading the XML document, as I used it in Blaise's answer. I'm loading it like this because I want to work with a node, not the whole doc. Even using the whole document I'm still having trouble when loading in this manner.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("[path to doc]/input.xml");
TestClass testClass = (TestClass) unmarshaller.unmarshal(doc);
我的XML看起来像这样:
I've got XML that looks like this:
<test>
<items>
<item type="cookie">cookie</item>
<item type="crackers">crackers</item>
</items>
</test>
一个班级:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "test")
public class TestClass
{
@XmlPath("items/item/text()")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
// getters, setters omitted
}
以上代码将起作用,无论我是否有 @XmlElement
,我得到一个包含[cookie,crackers]的ArrayList。
The above code will work whether or not I have @XmlElement
, and I get an ArrayList containing [cookie, crackers].
如果我将上面的声明更改为
If I change the declaration above to
@XmlPath("items/item/@type")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
我的ArrayList为空。
my ArrayList is empty.
我的最终目标是拥有属性,以便我的XML看起来像这样:
My ultimate goal is to just have attributes so my XML would look like this:
<test>
<items>
<item type="cookie"/>
<item type="crackers"/>
</items>
</test>
我正在尝试做什么,使用XPath提取属性列表,可能,以及如果是这样,怎么样?
Is what I'm trying to do, pull out a list of attributes using XPath, possible, and if so, how?
谢谢。
推荐答案
更新
我已经能够确认您所看到的问题( https://bugs.eclipse.org/353763 )。我们的EclipseLink 2.3.1和2.4.0流中添加了一个修复程序,可以从2011年8月4日开始的每晚下载页面获得:
I have been able to confirm the issue you are seeing (https://bugs.eclipse.org/353763). A fix has been added into our EclipseLink 2.3.1 and 2.4.0 streams and can be obtained from the nightly download page starting August 4th, 2011:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
解决方法:
您可以通过将 DocumentBuilderFactory
设置为名称空间来解决此问题:
You can workaround this issue by setting your DocumentBuilderFactory
to be namespace aware:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/forum6907225/input.xml");
testClass = (TestClass) unmarshaller.unmarshal(doc);
marshaller.marshal(testClass, System.out);
您正在正确进行映射(见下文) )。您是否已包含jaxb.properties文件以指定 EclipseLink MOXy 作为您的JAXB提供程序?:
You are doing the mapping correctly (see below). Have you included a jaxb.properties file to specify EclipseLink MOXy as your JAXB provider?:
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
测试类
package forum6907225;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "test")
public class TestClass
{
@XmlPath("items/item/@type")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
// getters, setters omitted
}
演示
package forum6907225;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.eclipse.persistence.Version;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(TestClass.class);
System.out.println(Version.getVersionString());
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum6907225/input.xml");
TestClass testClass = (TestClass) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(testClass, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<test>
<items>
<item type="cookie">cookie</item>
<item type="crackers">crackers</item>
</items>
</test>
输出
2.3.1.qualifier
<?xml version="1.0" encoding="UTF-8"?>
<test>
<items>
<item type="cookie"/>
<item type="crackers"/>
</items>
</test>
这篇关于使用MOXy和XPath,是否可以解组属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!