3

Edit: 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);

I've got XML that looks like this:

<test>
  <items>
    <item type="cookie">cookie</item>
    <item type="crackers">crackers</item>
  </items>
</test>

And a class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "test")
public class TestClass
{
  @XmlPath("items/item/text()")
  @XmlElement
  private ArrayList<String> itemList = new ArrayList<String>();

  // getters, setters omitted
}

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>();

my ArrayList is empty.

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>

Is what I'm trying to do, pull out a list of attributes using XPath, possible, and if so, how?

Thank you.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
Paul
  • 19,704
  • 14
  • 78
  • 96

1 Answers1

2

UPDATE

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:

Workaround:

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);

You are doing the mapping correctly (see below). Have you included a jaxb.properties file to specify EclipseLink MOXy as your JAXB provider?:

Test Class

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
}

Demo

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>

Output

2.3.1.qualifier
<?xml version="1.0" encoding="UTF-8"?>
<test>
   <items>
      <item type="cookie"/>
      <item type="crackers"/>
   </items>
</test>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Your example does work as shown, but when I combined it with how I loaded the document (see the edit I made to my question) then my output is this: `2.3.0.v20110604-r9504 ` I need to work with a node eventually - how should I load the document so I can pull out a node and run that through Moxy/JAXB? – Paul Aug 02 '11 at 21:26
  • I did a little more testing, trying to pull both the value and the attribute from each item. I can get the value but not the attribute still. – Paul Aug 03 '11 at 15:03
  • One last test...if I add an attribute to items, ``, I can retrieve it just fine, e.g. `@XmlPath("items/@type")`, just not from `item`. – Paul Aug 03 '11 at 15:07
  • @Paul - I have been able to confirm the issue that you are seeing, and have updated my answer with a workaround for it. – bdoughan Aug 03 '11 at 15:09
  • 1
    thanks for taking the time to look at this and to come up with a workaround. I am using MOXy as my JAXB provider. The xpath feature alone has saved me so much time! – Paul Aug 03 '11 at 16:27
  • 1
    @Paul - No problem, we're happy to help. You should be able to pick up a nightly download of our 2.3.1 stream with this fix tomorrow (http://www.eclipse.org/eclipselink/downloads/nightly.php). Also have you seen what's new wrt XmlPath in version 2.3 (http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html)? – bdoughan Aug 03 '11 at 16:46