Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:59:36+00:00 2026-06-03T21:59:36+00:00

I need to unmarshall an XML that has namespaces for the attributes, for instance

  • 0

I need to unmarshall an XML that has namespaces for the attributes, for instance

<license license-type="open-access" xlink:href="http://creativecommons.org/licenses/by/2.0/uk/"><license-p>

This attribute is defined as

@XmlAttribute(namespace = "http://www.w3.org/TR/xlink/")  
@XmlSchemaType(name = "anySimpleType")  
protected String href;  

But when I try to retrieve the href, it is null. What should I add/modify to the jaxb code in order to get the right value? I already tried to avoid namespaces but it did not work, still null. I also tried with @XmlAttribute(namespace = "http://www.w3.org/TR/xlink/", name = "href") but it did not work either.

The top of the XML file is:

<DOCTYPE article
  PUBLIC "-//NLM//DTD v3.0 20080202//EN" "archive.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" article-type="article">
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:59:37+00:00Added an answer on June 3, 2026 at 9:59 pm

    Below is an example of how to specify the namespace property on the @XmlAttribute annotation.

    input.xml

    <article xmlns:xlink="http://www.w3.org/1999/xlink">
        <license xlink:href="http://creativecommons.org/licenses/by/2.0/uk/"/>
    </article>
    

    License

    package forum10566766;
    
    import javax.xml.bind.annotation.XmlAttribute;
    
    public class License {
    
        private String href;
    
        @XmlAttribute(namespace="http://www.w3.org/1999/xlink")
        public String getHref() {
            return href;
        }
    
        public void setHref(String href) {
            this.href = href;
        }
    
    }
    

    Article

    package forum10566766;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Article {
    
        private License license;
    
        public License getLicense() {
            return license;
        }
    
        public void setLicense(License license) {
            this.license = license;
        }
    
    }
    

    Demo

    package forum10566766;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Article.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum10566766/input.xml");
            Article article = (Article) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(article, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <article xmlns:ns1="http://www.w3.org/1999/xlink">
        <license ns1:href="http://creativecommons.org/licenses/by/2.0/uk/"/>
    </article>
    

    Want to Control the Namespace Prefixes?

    If you want to control the namespace prefixes used when the document is marshalled to XML check out the following article:

    • http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

xml: <root> <element>value (something)</element> </root> I need to unmarshall this xml to two different
I have an xml that looks like this: <Root> <tag1>4</tag1> <tag2>aa</tag2> <tag3 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema>
Need to have a type-safe bag of items that all implement a generic interface.
In my Android app, I need to talk to a webserver that expects marshalled
need a little help with this one. I have a form that I am
I have a string of XML that looks like this: <e1 atr1=3 atr2=asdf> <e1b
We're using JAXB to unmarshall a fragment of XML which looks a bit like
I am working in an application where we need to save objects in XML
I need to make a Rest POST to a service that returns either a
I need to parse the java-objects which are passed through xml. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.