简体   繁体   English

JAXB上下文重命名名称空间并添加xml标头

[英]JAXB context to rename the namespace and add a xml header

Using JAXBContext, I have a package to create a XML header but the namespace is not generating properly.I need to change the XML from the first one to the second one. 使用JAXBContext,我有一个用于创建XML标头的包,但是命名空间生成不正确。我需要将XML从第一个更改为第二个。

JAXBContext jaxbContext = JAXBContext
                        .newInstance("com.oracle.xmlns.eos.v2"); 
Marshaller marshaller = jaxbContext.createMarshaller();

From : 来自:

<?xml version="1.0" encoding="UTF-8"?>
    <ns1:DPC
        xmlns:ns2="http://xmlns.oracle.com/EnterpriseObjects/V2"
        xmlns:ns1="http://url/xsd/DPC"
        xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing"
        xmlns:ns0="http://xmlns.oracle.com/EnterpriseObjects/IC/V1"/>

To: 至:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <ns1:DPC
            xmlns:ns1="http://url/xsd/DPC"
            xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing"
            xmlns:ns0="http://xmlns.oracle.com/EnterpriseObjects/IC/V1"
            xmlns:coreEB="http://xmlns.oracle.com/EnterpriseObjects/EB/V1"
            xmlns:cc="http://xmlns.oracle.com/EnterpriseObjects/V2"/>

Please suggest Is it possible to change the first to second.This needs to add xmlns:coreEB="http://xmlns.oracle.com/EnterpriseObjects/EB/V1" and rename the namespace from xmlns:ns2="http://xmlns.oracle.com/EnterpriseObjects/V2" to xmlns:cc="http://xmlns.oracle.com/EnterpriseObjects/V2" . 请建议是否可以将第一位更改为第二位。这需要添加xmlns:coreEB="http://xmlns.oracle.com/EnterpriseObjects/EB/V1"并从xmlns:ns2="http://xmlns.oracle.com/EnterpriseObjects/V2"重命名。 xmlns:ns2="http://xmlns.oracle.com/EnterpriseObjects/V2"xmlns:cc="http://xmlns.oracle.com/EnterpriseObjects/V2"

Both your variations are the same except that second has one more namespace http://xmlns.oracle.com/EnterpriseObjects/EB/V1 您的两个版本都相同,除了第二个版本还有一个名称空间http://xmlns.oracle.com/EnterpriseObjects/EB/V1

JAXB marshaller marshals namespaces from all known to its context classes or packages. JAXB编组器将其上下文类或包中所有已知的名称空间编组。 Your examples means that from your package com.oracle.xmlns.eos.v2 there is no references to the package containing classes with JAXB annotations for http://xmlns.oracle.com/EnterpriseObjects/EB/V1 namespace 您的示例意味着从com.oracle.xmlns.eos.v2包中没有对该包的引用,该包包含带有http://xmlns.oracle.com/EnterpriseObjects/EB/V1命名空间的JAXB批注的类

So, 所以,

first - double check your JAXB classes/packages structure, problem can be there. 首先-仔细检查您的JAXB类/包结构,问题可能在那里。

second - if you did not find discrepancy or not sure about that... try to add needed package for http://xmlns.oracle.com/EnterpriseObjects /EB/V1 namespace explicitly to JAXBContext separated by : like 第二个-如果您没有发现差异或不确定...请尝试将http://xmlns.oracle.com/EnterpriseObjects /EB/V1命名空间所需的包显式添加到以: like分隔的JAXBContext

JAXBContext jaxbContext = JAXBContext
                    .newInstance("com.oracle.xmlns.eos.v2:com.oracle.xmlns.enterpriseobjects.eb.v1"); 

You will have needed namespace in generated XML. 您将需要在生成的XML中使用名称空间。 Perhaps you will not have any elements or attributes for that namespace, because JAXB did not find it is needed, but namespace will be there. 也许您将没有该名称空间的任何元素或属性,因为JAXB并不需要它,但是名称空间就在那里。

Just keep in mind a package must be a JAXB, either with jaxb ObjectFactory class or jaxb.index file. 请记住,包必须是JAXB,带有jaxb ObjectFactory类或jaxb.index文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM