2013-06-12 2 views
4

xml 파일을 읽으려고합니다 (아래처럼).하지만 실행 파일이 있습니다. 이 문제를 어떻게 해결할 수 있습니까?JAXB로 Xml 파일 읽기

<?xml version="1.0" encoding="UTF-8"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= 
"file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml"> 
<log> 
    <property key="firstKey" value="firstValue"></property> 
    <property key="secoundKey" value="secoundKey"></property> 
    <property key="thirdKey" value="thirdValue"></property> 
</log> 

<env> 
    <property key="firstenv" value="fo"></property> 
    <property key="123" value="333"></property> 
</env> 
</config> 

이 내가 마지막으로이 내가 그것을

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"config"). Expected elements are (none) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1038) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:467) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:448) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169) 
    at firstExample.ReadXMLFilewithJAXB.main(ReadXMLFilewithJAXB.java:21) 

봉투 POJO 클래스를 얻을 수 있다는 예외입니다

XML 파일

public class ReadXMLFilewithJAXB { 
private static List<Property> customer; 

@SuppressWarnings("unchecked") 
public static void main(String[] args) { 

try { 

    File file = new File("c:\\FirstXML.xml"); 
    JAXBContext jaxbContext = JAXBContext.newInstance(Log.class); 
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
    customer = (List<Property>) jaxbUnmarshaller.unmarshal(file); 
} catch (JAXBException e) { 
    e.printStackTrace(); 
    } 

} 

}을 읽고 싶어 내 코드입니다

public class Env { 
    List<Property> properties2; 


    public Env() { 
    } 


    public Env(List<Property> properties2) { 
     super(); 
     this.properties2 = properties2; 
    } 


    public List<Property> getProperties2() { 
     return properties2; 
    } 

    public void setProperties2(List<Property> properties2) { 
     this.properties2 = properties2; 
    } 

로그인 POJO 클래스

@XmlRootElement 
public class Log { 
    List<Property> properties=new ArrayList<Property>();; 

    public Log(){ 

    } 

    public List<Property> getProperties() { 
     return properties; 
    } 

    public void setProperties(List<Property> properties) { 
     this.properties = properties; 
    } 
+0

? 'log' 요소 아래의 속성에만 관심이 있습니까? –

+0

나는 나의 XML 파일에서 2 개의 seperate List를 얻고 싶다. – Ritz

+0

태그에있는 "key"와 "value"모두에 대해 List1을 가져온 다음 태그에있는 모든 "key"와 "value"에 대해 List2를 가져 오는 것을 의미합니다. – Ritz

답변

4
당신은 (참조 @XmlRootElement 또는 @XmlElementDecl를 사용하여 XML 문서의 루트 요소와 클래스를 연결하도록해야
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"config"). Expected elements are (none) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631) 

: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html). 또는 Class 매개 변수를 사용하는 unmarshal 메소드 중 하나를 사용하여 언 마샬링 할 오브젝트 유형을 JAXB에 알릴 수 있습니다.


도메인 모델 (구성)

는 내가 어떤에서 당신이 Property 개체의 두 목록을 얻을 수있는 다음과 같은 도메인 클래스를 가지고 추천 할 것입니다.

import java.util.*; 
import javax.xml.bind.annotation.*; 

@XmlRootElement 
public class Config { 

    private List<Property> logProperties = new ArrayList<Property>(); 
    private List<Property> envProperties = new ArrayList<Property>(); 

    @XmlElementWrapper(name="log") 
    @XmlElement(name="property") 
    public List<Property> getLogProperties() { 
     return logProperties; 
    } 

    @XmlElementWrapper(name="env") 
    @XmlElement(name="property") 
    public List<Property> getEnvProperties() { 
     return envProperties; 
    } 

} 

데모

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Config.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum17059227/input.xml"); 
     Config config = (Config) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml"); 
     marshaller.marshal(config, System.out); 
    } 

} 

input.xml/출력은 XML 문서에서 얻을하려고하는 정보

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/mojalal/Desktop/FirstXSD.xml"> 
    <env> 
     <property key="firstenv" value="fo"/> 
     <property key="123" value="333"/> 
    </env> 
    <log> 
     <property key="firstKey" value="firstValue"/> 
     <property key="secoundKey" value="secoundKey"/> 
     <property key="thirdKey" value="thirdValue"/> 
    </log> 
</config> 
1

을 당신이 XML 모델을 설명하는, 당신은 (귀하의 경우는 <config> 요소의) 루트 엔티티로 시작해야합니다.

@XmlRootElement(name="config") 
class Config implements Serializable { 
    private Log log; 
    private Env env; 

    @XmlElement(name="log") 
    public Log getLog() { 
     return this.log; 
    } 

    @XmlElement(name="env") 
    public Env getEnv() { 
     return this.env; 
    } 

    // Setters are omitted 
} 

그리고 당신은 다음과 같은 XML을 구문 분석 :

JAXBContext jaxbContext = JAXBContext.newInstance(Config.class); 
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
Config config = (Config) jaxbUnmarshaller.unmarshal(file); 
if (config != null && config.getLog() != null) { 
    customer = config.getLog().getProperties(); 
} 
+0

나는 2 개를 얻고 싶다. seperate List 내 XML 파일에서 \t 태그에있는 "키"와 "값"모두에 대해 List1을 가져온 다음 태그에있는 모든 "키"와 "값"에 대해 List2를 가져 오는 것을 의미합니다. – Ritz

+0

이것은 내 문제입니다 .... – Ritz

+0

친애하는 "mikhail"..불행히도 고객 = config.getLog(). getProperties(). 크기(); "0"을 반환합니다. 뭐라구? – Ritz