Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

java - JAXB namespace prefixes missing

I have generated Java classes from XSD, all works fine from a unmarshalling point of view.

However, when I marshall from JAXB classes I get the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message xmlns="http://poc.cmc.com/ScreenLayout">
    <Data>
        <Type>Sample</Type>
     . . .
</message>

But I need

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns0:message xmlns:ns0="http://poc.cmc.com/ScreenLayout">
    <ns0:Data>
        <ns0:Type>Sample</ns0:Type>
    . . .

how can I control that from Java?

Thanks a lot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the @XmlSchema annotation on a package-info class to assign a prefix to the namespace:

@XmlSchema(
    namespace = "http://poc.cmc.com/ScreenLayout",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns={@XmlNs(prefix="ns0", namespaceURI="http://poc.cmc.com/ScreenLayout")})    
package your.package;


import javax.xml.bind.annotation.*;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...