javax.xml.bind.UnmarshalException: unexpected element (uri:“”, local:“Group”)

前端 未结 14 1094
终归单人心
终归单人心 2020-12-02 10:13
unexpected element (uri:\"\", local:\"Group\"). Expected elements are <{}group>

Meet an exception when unmarshalling from xml



        
相关标签:
14条回答
  • 2020-12-02 10:51

    You need to put package-info.java class in package of contextPath and put below code in same class:

    @javax.xml.bind.annotation.XmlSchema(namespace = "https://www.namespaceUrl.com/xml/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.test.valueobject;
    
    0 讨论(0)
  • 2020-12-02 10:51

    Same to me. The name of the mapping class was Mbean but the tag root name was mbean so I had to add the annotation:

    @XmlRootElement(name="mbean")
    public class MBean { ... }
    
    0 讨论(0)
  • 2020-12-02 10:51

    I had the same issue. I added following attributes to <xs:schema..> elementFormDefault="qualified" attributeFormDefault="unqualified"

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns="http://www.example.com/schemas/ArrayOfMarketWithStations"
        targetNamespace="http://www.example.com/schemas/ArrayOfMarketWithStations" 
        elementFormDefault="qualified" attributeFormDefault="unqualified" >
    

    and re-generated java classes by running xjc, which corrected package-info.java.

    @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.com/schemas/ArrayOfMarketWithStations", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    

    This fixed the issue for me.

    0 讨论(0)
  • 2020-12-02 10:56

    I had the same issue, my problem was, that I had two different webservices with two different wsdl-files. I generated the sources in the same package for both webservices, which seems to be a problem. I guess it's because of the ObjectFactory and perhaps also because of the package-info.java - because those are only generated once.

    I solved it, by generating the sources for each webservice in a different package. This way, you also have two different ObjectFactories and package-info.java files.

    0 讨论(0)
  • 2020-12-02 10:58

    None of the solutions mentioned here worked for me, I was still getting:

    Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"java:XXX.XX.XX.XXX", local:"XXXXX")

    After lot of research through other sites below code worked for me-

    FileInputStream fis = new FileInputStream("D:/group.xml");
    SOAPMessage message = factory.createMessage(new MimeHeaders(), fis);
    JAXBContext jc = JAXBContext.newInstance(Group.class);
    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement<Group> r = u.unmarshal(message.getSOAPBody().extractContentAsDocument(), Group.class);
    Group group = r.getValue();
    
    0 讨论(0)
  • 2020-12-02 10:59

    I had the same problem.. It helped me, I'm specify the same field names of my classes as the tag names in the xml file (the file comes from an external system).

    For example:

    My xml file:

    <Response>
      <ESList>
         <Item>
            <ID>1</ID>
            <Name>Some name 1</Name>
            <Code>Some code</Code>
            <Url>Some Url</Url>
            <RegionList>
               <Item>
                  <ID>2</ID>
                  <Name>Some name 2</Name>
               </Item>
            </RegionList>
         </Item>
      </ESList>
    </Response>
    

    My Response class:

    @XmlRootElement(name="Response")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Response {
        @XmlElement
        private ESList[] ESList = new ESList[1]; // as the tag name in the xml file..
    
        // getter and setter here
    }
    

    My ESList class:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name="ESList")
    public class ESList {
        @XmlElement
        private Item[] Item = new Item[1]; // as the tag name in the xml file..
    
        // getters and setters here
    }
    

    My Item class:

    @XmlRootElement(name="Item")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Item {
        @XmlElement
        private String ID; // as the tag name in the xml file..
        @XmlElement
        private String Name; // and so on...
        @XmlElement
        private String Code;
        @XmlElement
        private String Url;
        @XmlElement
        private RegionList[] RegionList = new RegionList[1];
    
        // getters and setters here
    }
    

    My RegionList class:

    @XmlRootElement(name="RegionList")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class RegionList {
        Item[] Item = new Item[1];
    
        // getters and setters here
    }
    

    My DemoUnmarshalling class:

    public class DemoUnmarshalling {
        public static void main(String[] args) {
            try {
                File file = new File("...");
    
                JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                jaxbUnmarshaller.setEventHandler(
                    new ValidationEventHandler() {
                        public boolean handleEvent(ValidationEvent event ) {
                            throw new RuntimeException(event.getMessage(),
                                event.getLinkedException());
                        }
                    }
                );
    
                Response response = (Response) jaxbUnmarshaller.unmarshal(file);
    
                ESList[] esList = response.getESList();
                Item[] item = esList[0].getItem();
                RegionList[] regionLists = item[0].getRegionList();
                Item[] regionListItem = regionLists[0].getItem();
    
                System.out.println(item[0].getID());
                System.out.println(item[0].getName());
                System.out.println(item[0].getCode());
                System.out.println(item[0].getUrl());
                System.out.println(regionListItem[0].getID());
                System.out.println(regionListItem[0].getName());
    
            } catch (JAXBException e) {
                e.printStackTrace();
            }
        }
    }
    

    It gives:

    1
    Some name 1
    Some code
    Some Url
    2
    Some name 2
    
    0 讨论(0)
提交回复
热议问题