Кажется, есть несколько тем справки по этой проблеме, но я не нашел решение, для которого меня беспокоит.jaxb неожиданный элемент error local: root
я должен работать со структурой XML ниже:
<Customer xmlns="http://www.somedomain.com/customer-example">
<Name>David Brent</Name>
<Notes>Big time</Notes>
</Customer>
Он имеет другие поля, но я не смог заставить его работать даже с этой минимальной настройкой.
Мой POJO:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"notes"
})
@XmlRootElement(name = "Customer")
public class Customer {
@XmlElement(name = "Name", required = true)
public String name;
@XmlElement(name = "Notes", required = true)
public String notes;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
...
...
}
И клиент:
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer)unmarshaller.unmarshal(new File("Data.xml"));
System.out.println("Customer: "+customer.getName());
}
Это бросает исключение:
Exception in thread "main" javax.xml.bind.UnmarshalException:
unexpected element (uri:"", local:"root"). Expected elements are <{}Customer>
Что такое локальное: корень ??? Если я пытаюсь разобрать его с другим способом
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource streamSource = new StreamSource("Data.xml");
JAXBElement<Customer> customer = (JAXBElement<Customer>).
unmarshaller.unmarshal(streamSource, Customer.class);
customer.getValue.getName(); //is null
ли эта проблема есть, что делать с определением Xmlns в моем XML ??
Использование Netbeans 7.3.1 с Java 1.7 OpenJDK
Блейн, ваш блог очень помог. Я получил его работу –