标签: jaxb

困难的JAXB映射

我试图在更大的文档中映射以下XML结构,显然这不是XML的最大用途,但这是我必须使用的.

为清晰起见简化示例:

  <details>
    <pictures>
      <picture1>
        http://domain.com/path/picture1.jpg
      </picture1>
      <picture2>
        http://domain.com/path/picture2.jpg
      </picture2>
      <picture3>
        http://domain.com/path/picture3.jpg
      </picture3>
      <picture4>
        http://domain.com/path/picture4.jpg
      </picture4>
      <picture5>
        http://domain.com/path/picture5.jpg
      </picture5>
      <picture6>
        http://domain.com/path/picture6.jpg
      </picture6>
      <picture7>
        http://domain.com/path/picture7.jpg
      </picture7>
    </pictures>
  </details>
Run Code Online (Sandbox Code Playgroud)

该文档有一个DTD,声明最多可以有30个不同的图片元素,编号为1-30 <picutre[n]/>

我想做的是,而不是为这些元素创建30个不同的类,分别称为Picture1,Picture2,Picture3 ......等等.我只想使用一个Picture类并将其用于所有30个唯一元素名称.

以下是我到目前为止所尝试的内容.

@XmlRootElement
public class Details {

   ...

   @XmlElementWrapper
   @XmlElementRefs({
        @XmlElementRef( name="picture1", type=Picture.class ),
        @XmlElementRef( name="picture2", type=Picture.class ),
        @XmlElementRef( name="picture3", type=Picture.class ),
        @XmlElementRef( name="picture4", type=Picture.class ),
        @XmlElementRef( name="picture5", type=Picture.class ),
        @XmlElementRef( name="picture6", type=Picture.class ),
        @XmlElementRef( name="picture7", type=Picture.class ),
        @XmlElementRef( name="picture8", type=Picture.class ),
        @XmlElementRef( name="picture9", type=Picture.class ),
        @XmlElementRef( name="picture10", type=Picture.class ), …
Run Code Online (Sandbox Code Playgroud)

java jaxb

1
推荐指数
1
解决办法
1021
查看次数

将SAX与JAXBContext一起使用

我正在尝试将XML数据反序列化为新创建的Java内容树:

我正在使用SAX,在src\main\java\Summaries.java下有Java类,我正在尝试简单地打印提取的文档:

String xmlPath = "C:\\workspace-sts-2.8.0.RELEASE\\RESTClient\\src\\main\\resources\\dailySummary.xml";
String xmlPath2 = "/dailySummary.xml";
String xmlPath3 = "/src/main/resources/dailySummary.xml";

InputStream inputStream = null;
InputSource inputSource = null;
Unmarshaller unmarshaller = null;
JAXBContext jc = null;
try 
{

// read from a file
// or try xmlPath1 or try xmlPath3
inputStream = RESTtestclient.class.getClassLoader().getResourceAsStream(xmlPath2);

inputSource = new InputSource(inputStream);
SAXParserFactory sax = SAXParserFactory.newInstance();
sax.setNamespaceAware(true);
XMLReader reader = sax.newSAXParser().getXMLReader();
SAXSource saxSource= new SAXSource(reader, inputSource);

// use jAXB
Class[] classes = new Class[1];
classes[0]= Summaries.class;
jc = JAXBContext.newInstance(classes); …
Run Code Online (Sandbox Code Playgroud)

sax jaxb saxparser

1
推荐指数
1
解决办法
7250
查看次数

创建名为'org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter'的bean时出错

我正在尝试一个简单的弹簧Web服务应用程序.我已正确配置应用程序,但当我尝试访问wsdl文件时,我收到以下错误:

    at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_07]

17:24:35,409 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/springWsTest]] (http--127.0.0.1-8080-1) Initializing Spring FrameworkServlet 'webservice'
17:24:35,419 INFO  [org.springframework.ws.transport.http.MessageDispatcherServlet] (http--127.0.0.1-8080-1) FrameworkServlet 'webservice': initialization started
17:24:35,428 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] (http--127.0.0.1-8080-1) Refreshing WebApplicationContext for namespace 'webservice-servlet': startup date [Tue Jul 03 17:24:35 BST 2012]; parent: Root WebApplicationContext
17:24:35,443 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (http--127.0.0.1-8080-1) Loading XML bean definitions from ServletContext resource [/WEB-INF/ws-config.xml]
17:24:35,541 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (http--127.0.0.1-8080-1) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:24:35,595 INFO  [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (http--127.0.0.1-8080-1) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:24:35,655 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] …
Run Code Online (Sandbox Code Playgroud)

java spring web-services jaxb marshalling

1
推荐指数
1
解决办法
1万
查看次数

我可以将当​​前的JAXB模型转换为其他JAXB模型吗?

我有两个XML文件file1.xmlfile2.xml.我也有每个文件的模式,file1.xsdfile2.xsd尊重.

xjc用来创建基于file1.xsd(称为model(1))的java类,并基于file2.xsd(称为model(2))创建类.

我使用model(1)来解析文件JAXB库中的file1.xml .我想将此数据从模型(1)转换为模型(2),然后将转换后的数据封送到file2.xml

是否可以将数据从模型(1)转换为模型(2)?我可以合并ObjectFactories吗?我该怎么做?

java xml xsd jaxb xjc

1
推荐指数
1
解决办法
1792
查看次数

import com.sun.xml.internal.ws.client.ClientTransportException,无法读取此导入

我无法解决这个问题,我尝试导入com.sun.xml.internal.ws.client.ClientTransportException,但我无法导入它.

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 404: Not Found

at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.checkStatusCode(HttpTransportPipe.java:296)

at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:245)

at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.j
ava:203)

at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:122)

at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:95)

at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)

at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)

at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)

at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)   

at com.sun.xml.internal.ws.client.Stub.process(Stub.java:308)

at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:146)

at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98)

at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)

at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:129)

java import jaxb

1
推荐指数
1
解决办法
1万
查看次数

休息 - 订购GET响应xml

我从rest GET方法得到这个简单的xml.Restful服务是用java编写的.

   <gear>
      <price>355.95929799818884</price>
      <idGear>1</idGear>
      <year>1985</year>
      <name>GPS</name>
  </gear>
Run Code Online (Sandbox Code Playgroud)

我的问题是如何改变xml的顺序?我想将idGear作为第一个节点.这是基本的还是比我想的更复杂?感谢帮助?

更新:

@XmlRootElement
@Entity(name="gear")
public class Gear{

    @Id
    @GeneratedValue
    int idGear;
    private String name;
    private int year;
    private double price;
      //getters, setters
Run Code Online (Sandbox Code Playgroud)

java xml rest get jaxb

1
推荐指数
1
解决办法
1110
查看次数

字符串到joda LocalDate,格式为"dd-MMM-yy"

我正在使用JAXB和joda时间2.2.将数据从Mysql备份到XML并将其还原.在我的表中,我有一个Date属性,格式为"16-Mar-05".我成功地将其存储在XML中.但是当我想从XML中读取它并将其放回Mysql表中时,我无法获得正确的格式.

这是我的XMLAdapter类,这里的unmarshal方法输入String是"16-Mar-05",但我不能以"16-Mar-05"的格式获取localDate变量,虽然我将模式设置为"dd- MMM-YY".我发布了我尝试过的所有选项,如何在16d-05-05格式的"dd-MMM-yy"中获取我的localDate?

谢谢!!

public class DateAdapter extends XmlAdapter<String, LocalDate> {

// the desired format
private String pattern = "dd-MMM-yy";

@Override
public String marshal(LocalDate date) throws Exception {
    //return new SimpleDateFormat(pattern).format(date);
    return date.toString("dd-MMM-yy");
}

@Override
public LocalDate unmarshal(String date) throws Exception {
    if (date == null) {
        return null;
    } else {
        //first way
        final DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MMM-yy");
        final LocalDate localDate2 = dtf.parseLocalDate(date);

        //second way
        LocalDate localDate3 = LocalDate.parse(date,DateTimeFormat.forPattern("dd-MMM-yy"));

        //third way
        DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("dd-MMM-yy");
        DateTime dateTime = FORMATTER.parseDateTime(date); …
Run Code Online (Sandbox Code Playgroud)

java mysql jaxb jodatime

1
推荐指数
1
解决办法
2万
查看次数

JAXB和UTF-8解组异常"2字节UTF-8序列的无效字节2"

我已经阅读了一些SO答案,说JAXB有一个错误,它归咎于XML的性质,导致它无法使用UTF-8.我的问题是,那么解决方法是什么?我可能会将用户输入的unicode字符复制并粘贴到我需要保存,编组,解组并在其他地方重新显示的数据字段中.

(更新)更多背景信息:

Candidate c = new Candidate();
c.addSubstitution("3 4ths", "\u00BE");
c.addSubstitution("n with tilde", "\u00F1");
    c.addSubstitution("schwa", "\u018F");
    c.addSubstitution("Sigma", "\u03A3");
    c.addSubstitution("Cyrillic Th", "\u040B");     
    jc = JAXBContext.newInstance(Candidate.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    marshaller.marshal(c, os);
    String xml = os.toString();
    System.out.println(xml);    
    jc = JAXBContext.newInstance(Candidate.class);
    Unmarshaller jaxb = jc.createUnmarshaller();
    ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
    Candidate newCandidate = (Candidate) jaxb.unmarshal(is);
    for(Substitution s:c.getSubstitutions()) {
        System.out.println(s.getSubstitutionName() + "='" + s.getSubstitutionValue() + "'");
    }
Run Code Online (Sandbox Code Playgroud)

这是一个小测试位,我把它们放在一起.我得到的确切字符并非完全由我控制.用户可以将带有波形符号的N粘贴到字段中或其他任何内容.

java xml unicode jaxb

1
推荐指数
1
解决办法
1万
查看次数

无法转换为类型spring.oxm.marshaller:找不到匹配的编辑器或转换策略

我长期以来一直在努力解决这个错误,谷歌搜索,看看网络上的例子,仍然没有开始工作.说实话,我不明白为什么我的项目会抛出这个错误.

Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.oxm.Marshaller] for property 'marshaller': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    ... 52 more)
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.TestCase.fail(TestCase.java:227)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:100)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    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)
Run Code Online (Sandbox Code Playgroud)

所以我的设置是Spring Web Services,Jaxb2,maven.我已经预定义了xsd文件并通过jaxb2 maven插件从它们生成了java类.

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>schema1-xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>src/main/resources/</schemaDirectory> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-ws jaxb

1
推荐指数
1
解决办法
1万
查看次数

在Classpath上运行时加载java.io.File

我有两个jar(main.jar和schema.jar),我需要从main.jar中的一个类中的schema.jar中读取一个模式文件(位于src/main/resources/sample.xsd中).main.jar在其类路径上将schema.jar作为maven依赖项.当我使用this.getClass().getClassLoader().getResourceAsStream时,我能够找到sample.xsd文件,但我需要一个java.io.File.提供java.io.File参数的内存效率最高的方法是什么?

main.jar文件:

InputStream in = this.getClass().getClassLoader()
        .getResourceAsStream("/resources/sample.xsd");

Sample sample = new Sample();
//set sample data here
 Marshaller marshaller = JAXBContext.newInstance(Sample.getClass()).createMarshaller();
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final Schema s = schemaFactory.newSchema(new File("/resources/sample.xsd"));
marshaller.setSchema(s);
    marshaller.marshal(Sample, XML);
Run Code Online (Sandbox Code Playgroud)

java schema jaxb

1
推荐指数
1
解决办法
2569
查看次数

标签 统计

jaxb ×10

java ×9

xml ×3

spring ×2

get ×1

import ×1

jodatime ×1

marshalling ×1

mysql ×1

rest ×1

sax ×1

saxparser ×1

schema ×1

spring-ws ×1

unicode ×1

web-services ×1

xjc ×1

xsd ×1