2013-12-16 2 views
3

xml로 언 마샬링에 대한 테스트를하고 싶습니다. 그래서 책에있는 코드를 작성합니다. 코드는 다음과 같이되어 ..XML 스키마 사이트가 존재하지 않습니다 .. (XML을 언 마샬링 할 때)

sqlmap.xml

<?xml version="1.0" encoding="UTF-8"?> 
<sqlmap xmlns="http://www.example.org/sqlmap/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema" 
xsi:schemaLocation="http://www.example.org/sqlmap/ http://www.example.org/sqlmap/sqlmap.xsd"> 

<sql key="add">insert</sql> 
<sql key="get">select</sql> 
<sql key="delete">delete</sql> 
</sqlmap> 

JaxbTest.java

package springbook.learningtest.jdk.jaxb; 

import java.io.IOException; 
import java.util.List; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 



import org.junit.Test; 

import springbook.user.sqlservice.jaxb.SqlType; 
import springbook.user.sqlservice.jaxb.Sqlmap; 
import static org.hamcrest.CoreMatchers.is; 
import static org.junit.Assert.assertThat; 

public class JaxbTest { 
    @Test 
    public void readSqlmap() throws JAXBException, IOException{ 
     String contextPath = Sqlmap.class.getPackage().getName(); 
     JAXBContext context = JAXBContext.newInstance(contextPath); 
     Unmarshaller unmarshaller = context.createUnmarshaller(); 

     Sqlmap sqlmap = (Sqlmap) unmarshaller.unmarshal(getClass().getResourceAsStream("sqlmap.xml")); 
     List<SqlType> sqlList = sqlmap.getSql(); 

     assertThat(sqlList.size(), is(3)); 
     assertThat(sqlList.get(0).getKey(), is("add")); 
     assertThat(sqlList.get(0).getValue(), is("insert")); 
     assertThat(sqlList.get(1).getKey(), is("get")); 
     assertThat(sqlList.get(1).getValue(), is("select")); 
     assertThat(sqlList.get(2).getKey(), is("delete")); 
     assertThat(sqlList.get(2).getValue(), is("delete")); 
    } 
} 

SqlMap.java

package springbook.user.sqlservice.jaxb; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 



@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "sql" 
}) 
@XmlRootElement(name = "sqlmap") 
public class Sqlmap { 

    @XmlElement(required = true) 
    protected List<SqlType> sql; 


    public List<SqlType> getSql() { 
     if (sql == null) { 
      sql = new ArrayList<SqlType>(); 
     } 
     return this.sql; 
    } 

} 

SqlType.java

package springbook.user.sqlservice.jaxb; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 



@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "sqlType", propOrder = { 
    "value" 
}) 
public class SqlType { 

    @XmlValue 
    protected String value; 
    @XmlAttribute(name = "key", required = true) 
    protected String key; 


    public String getValue() { 
     return value; 
    } 


    public void setValue(String value) { 
     this.value = value; 
    } 


    public void setKey(String value) { 
     this.key = value; 
    } 

} 

그러나 네임 스페이스 사이트 (http://example.org)가 더 이상 존재하지 않으므로 문제가 발생했습니다. 그래서 컴파일 할 수 없습니다. 오류 코드는 다음과 같습니다.

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.example.org/sqlmap/", local:"sqlmap"). Expected elements are <{http://www.example.org/sqlmap}sqlmap> 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) 
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) 
    at springbook.learningtest.jdk.jaxb.JaxbTest.readSqlmap(JaxbTest.java:26) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

XML에 대한 지식이 없습니다. 컴파일 만하고 싶습니다. 뭘해야합니까?

+0

'Sqlmap' 클래스와'SqlType' 클래스를 게시 할 수 있습니까? –

+0

나는 그것을 배치했다. 고맙습니다. – velmont

답변

0

XML의 요소가 해당 Java 클래스와 일치하지 않습니다. 귀하의 XML은 XML 네임 스페이스에 속한 것으로 선언합니다.

xmlns="http://www.example.org/sqlmap/" 

그러나 클래스는 아무 것도 말하지 않습니다.

당신은 루트 및 중첩 된 요소 (둘 다 네임 스페이스에 속하는) 모두에 대한 네임 스페이스

XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { "sql" }) 
@XmlRootElement(name = "sqlmap", namespace = "http://www.example.org/sqlmap/") 
public class Sqlmap { 
    @XmlElement(namespace = "http://www.example.org/sqlmap/") 
    private List<SqlType> sql; 

    public List<SqlType> getSql() { 
     return sql; 
    } 

    public void setSql(List<SqlType> sql) { 
     this.sql = sql; 
    } 
} 

을 지정해야합니다.

+0

그것은 잘 간다 ..! 고맙습니다! – velmont

+0

@velmont - 오류 메시지는 네임 스페이스 'http : // www.example.org/sqlmap'을 매핑했음을 나타냅니다. XML 문서'http://www.example.org/sqlmap '과 일치하지 않습니다. /'(여분의 슬래시). 이 해결책이 효과적 일지 모르지만'package-info' 클래스에있는'@XmlSchema'에서 한 줄만 변경하면 더 쉽습니다. JAXB 및 네임 스페이스에 대한 자세한 내용은 http://blog.bdoughan.com/2010/08/jaxb-namespaces.html –

+1

@velmont 예, Blaise가 옳습니다. 이미 네임 스페이스를 어딘가에 선언했지만 이미 잘못 알고있는 것을 눈치 채지 못했습니다. –

0
xsi:schemaLocation="http://www.example.org/sqlmap/ ../../../../../sqlmap.xsd "> 
                |       | 
                |       | 
                here     and here 

약간의 공간이 있습니다.

+0

수정했습니다. 하지만 너무 작동하지 않습니다 .. 감사합니다 – velmont

0

XML 네임 스페이스는 일반적으로 URL로 구성되지만 필수는 아니지만 실제 위치와 일치 할 필요는 없습니다. example.org은 설명을 위해 예약 된 도메인입니다.

XML 문서의 네임 스페이스 끝에 /이 있지만 메타 데이터에는 없습니다. 당신은 다음에 문서를 변경하여이 문제를 해결할 수 있습니다

<?xml version="1.0" encoding="UTF-8"?> 
<sqlmap xmlns="http://www.example.org/sqlmap" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema" 
    xsi:schemaLocation="http://www.example.org/sqlmap/ http://www.example.org/sqlmap/sqlmap.xsd"> 
    <sql key="add">insert</sql> 
    <sql key="get">select</sql> 
    <sql key="delete">delete</sql> 
</sqlmap> 

또는 package-info 클래스에 @XmlSchema의 이름 공간.

+1

나를 고맙게 대해 주셔서 감사합니다 : D 조 – velmont

관련 문제