unmarshal example

Hi,

I am trying below example on JAXB unmarshal

http://www.java2s.com/Code/Java/JDK-6/UnmarshallFromXMLusingJAXB.htm

i am seeing compilation error as below for Item
Multiple markers at this line
      - Item cannot be resolved
       to a type
      - Item cannot be resolved
       to a type

Do i need to use below XSD given in that link or is that is one that is generated as output?

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="item" type="Item"/>
    <xsd:complexType name="Item">
        <xsd:sequence>
            <xsd:element name="code" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="price" type="xsd:double"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Open in new window


Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
depends what your item.xml looks like
0
gudii9Author Commented:
that link did not give item.xml for some reason. Where can i find it?
please advise
0
gurpsbassiCommented:
that link did not give item.xml for some reason. Where can i find it?

no idea. try emailing the admin of the site.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gudii9Author Commented:
i see download link and xml in that zip file

<?xml version="1.0"?>
<item>
          <code>I001</code>
          <name>First item</name>
          <price>100000</price>
</item>
0
gurpsbassiCommented:
Re-reading your question again,

You said:

i am seeing compilation error as below for Item

Are you saying, you have simply copy and pasted that code and expected it to work???????
I hope not.

If you're getting Item cannot be resolved to a type - its correct. Where is your Item.java class?
0
gudii9Author Commented:
package generated;
import generated.Item;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class UnmarshallingDemo {

    public static void main (String [] args) {
        try {
            JAXBContext jc = JAXBContext.newInstance ("generated");

            Unmarshaller u = jc.createUnmarshaller ();

           File f = new File ("item.xml");
           JAXBElement element = (JAXBElement) u.unmarshal (f);

           Item item = (Item) element.getValue ();
           System.out.println (item.getCode ());
           System.out.println (item.getName ());
           System.out.println (item.getPrice ());
       } catch (JAXBException e) {
           e.printStackTrace ();
       }
   }
}

Open in new window


i thought Item.java should be generated from the item.xml
i see that zip file i downloaded has below generated class
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.0 in JDK 1.6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2007.08.20 at 03:34:05 PM PDT 
//


package generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for Item complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Item">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="code" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="price" type="{http://www.w3.org/2001/XMLSchema}double"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Item", propOrder = {
    "code",
    "name",
    "price"
})
public class Item {

    @XmlElement(required = true)
    protected String code;
    @XmlElement(required = true)
    protected String name;
    protected double price;

    /**
     * Gets the value of the code property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCode() {
        return code;
    }

    /**
     * Sets the value of the code property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCode(String value) {
        this.code = value;
    }

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

    /**
     * Gets the value of the price property.
     * 
     */
    public double getPrice() {
        return price;
    }

    /**
     * Sets the value of the price property.
     * 
     */
    public void setPrice(double value) {
        this.price = value;
    }

}

Open in new window

0
gurpsbassiCommented:
Ok, I wasn't aware there was a zip file.
I'll check it out later.
0
gurpsbassiCommented:
Right well I downloaded the zip file you mentioned and got the code into eclipse.
It all looks fine to me.

I compiles and runs perfectly.

You are facing some other problem.

1)Try doing a project clean.
2) send me a screenshot of your eclipse showing your package explorer with the classes.
0
gudii9Author Commented:
sure
0
gudii9Author Commented:
when i import zip file got attached error. I will try unzipped with 'select root directory' top option as in screenshot.

'select archive file' always always gives me same kind of attached error.
please advise
impZip.png
0
gudii9Author Commented:
first option also same error as attached. How did you imported. Please provide detailed steps to follow starting from downloading and unzipping and selecting right option in eclipse with screenshots please.
impZip2.png
0
gurpsbassiCommented:
when i import zip file got attached error

WHY are you trying to import a zip into eclipse?

simply unzip it into a directory and set up a new java project containing the UNZIPPED source.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
simply unzip it into a directory and set up a new java project containing the UNZIPPED source.
how to do this step.

Just create empty java project with some same TestProject.

Then copy paste contents of UNZIPPED source manually(only java files or config files or all at root level)?
0
gudii9Author Commented:
i did manually
i got outpu
I001
First item
100000.0

I followed below link and generated my own XSD and ObjectFactory class and used them to unmarshal as attached

http://www.javacodegeeks.com/2014/05/generate-your-jaxb-classes-in-a-second-with-xjc.html

what is the importance of generated item.java and ObjectFactory.java for unmarshalling given XML to java object.

How Hibernate mapping files like hbm and pojo and session factory related to the JAXB concept here?

Using eclipse how to invoke xjc command to generate the item.java and ObjectFactory.java at given package rather than doing manually from command line
please advise
xjc2.png
xjc.png
0
gurpsbassiCommented:
perhaps post your questions as new questions rather then polluting the same thread.

Can we close this issue now since it works for you?
0
gudii9Author Commented:
sure
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.