{"id":2484,"date":"2009-02-02T17:14:24","date_gmt":"2009-02-02T16:14:24","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=2484"},"modified":"2009-02-02T17:14:24","modified_gmt":"2009-02-02T16:14:24","slug":"customize-namespace-prefix-when-marshalling-with-jaxb","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/","title":{"rendered":"Customize namespace prefix when marshalling with Jaxb"},"content":{"rendered":"<p>I was looking a long time on how to set the prefix for a namespace when marshalling an object to xml using JAXB. If you don&#8217;t do anything JAXB will write random namespace prefixes (like ns2, ns3).?<\/p>\n<div style=\"background-color: #4b08a1;color: white;font-weight: bold;padding: 10px;margin: 10px\">Note:<br \/>\nThis is a repost of a blog item that was originally posted in the Func knowledge base by Martin Tilma on Februari 2, 2009.<\/div>\n<p>First I found: <a href=\"https:\/\/jaxb.dev.java.net\/guide\/Changing_prefixes.html\">https:\/\/jaxb.dev.java.net\/guide\/Changing_prefixes.html<\/a> but with that solution you have to implement an internal abstract class <strong>NamespacePrefixMapper<\/strong>.<\/p>\n<p>After re-reading the Javadoc of the Marshaller I found the method <strong>marshal(Object jaxbElement, XMLStreamWriter writer)<\/strong> and the <strong>XMLStreamWriter<\/strong> interface contains a method <strong>setPrefix(String prefix, String uri)<\/strong><\/p>\n<p>A little code example:<\/p>\n<p><pre class=\"brush: java; title: ; notranslate\" title=\"\">&lt;br&gt;\nJAXBContext context = JAXBContext.newInstance(object.getClass());&lt;\/p&gt;\n&lt;p&gt;XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance()&lt;br&gt;\n.createXMLStreamWriter(writer);&lt;br&gt;\nxmlStreamWriter.setPrefix(&quot;func&quot;, &quot;http:\/\/www.func.nl&quot;);&lt;br&gt;\nMarshaller marshaller = context.createMarshaller();&lt;\/p&gt;\n&lt;p&gt;marshaller.marshal(object, xmlStreamWriter);&lt;br&gt;\n<\/pre><\/p>\n<p><strong>update:<\/strong> I have one remaining issue, for some reason the namespace declaration isn&#8217;t written to the XML when using setPrefix. If you remove the call to setPrefix it writes the namespace declaration.<\/p>\n<p><strong>update2:<\/strong> After digging around I found that my project has dependency on &#8220;WoodSToX XML-processor&#8221; which specifies its own <strong>XMLOutputFactory<\/strong> (see the META-INF\/services directory in the jar).<\/p>\n<p>To fix my issue I wrote a <strong>XMLStreamWriter<\/strong> wrapper that calls <strong>writeNamespace(String prefix, String namespaceURI)<\/strong> to write the declaration.<\/p>\n<p>The <a href=\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2010\/09\/MarshalUtil.java_.txt\">attachment (MarshalUtil.java_.txt)<\/a> contains a utility class to marshal and unmarshal.<\/p>\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/bit.ly\/3BAo305\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"256\" src=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\" alt=\"\" class=\"wp-image-20303\" srcset=\"https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png 1024w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-300x75.png 300w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-768x192.png 768w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1536x384.png 1536w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-2048x512.png 2048w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1920x480.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I was looking a long time on how to set the prefix for a namespace when marshalling an object to xml using JAXB. If you don&#8217;t do anything JAXB will write random namespace prefixes (like ns2, ns3).? Note: This is a repost of a blog item that was originally posted in the Func knowledge base [&hellip;]<\/p>\n","protected":false},"author":78,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[31,10],"tags":[11,152,153,154,52],"class_list":["post-2484","post","type-post","status-publish","format-standard","hentry","category-java","category-development","tag-java","tag-jaxb","tag-namespace","tag-prefix","tag-xml"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Customize namespace prefix when marshalling with Jaxb - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Customize namespace prefix when marshalling with Jaxb - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"I was looking a long time on how to set the prefix for a namespace when marshalling an object to xml using JAXB. If you don&#8217;t do anything JAXB will write random namespace prefixes (like ns2, ns3).? Note: This is a repost of a blog item that was originally posted in the Func knowledge base [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2009-02-02T16:14:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\" \/>\n<meta name=\"author\" content=\"Martin Tilma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Martin Tilma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/\",\"url\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/\",\"name\":\"Customize namespace prefix when marshalling with Jaxb - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\",\"datePublished\":\"2009-02-02T16:14:24+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/8b8a3958adbd9f58d1c257c622fe0f4e\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage\",\"url\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\",\"contentUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Customize namespace prefix when marshalling with Jaxb\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/8b8a3958adbd9f58d1c257c622fe0f4e\",\"name\":\"Martin Tilma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b4ccfbaec8216c8fa31c753299573cae?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b4ccfbaec8216c8fa31c753299573cae?s=96&d=mm&r=g\",\"caption\":\"Martin Tilma\"},\"url\":\"https:\/\/trifork.nl\/blog\/author\/martin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Customize namespace prefix when marshalling with Jaxb - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/","og_locale":"en_US","og_type":"article","og_title":"Customize namespace prefix when marshalling with Jaxb - Trifork Blog","og_description":"I was looking a long time on how to set the prefix for a namespace when marshalling an object to xml using JAXB. If you don&#8217;t do anything JAXB will write random namespace prefixes (like ns2, ns3).? Note: This is a repost of a blog item that was originally posted in the Func knowledge base [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/","og_site_name":"Trifork Blog","article_published_time":"2009-02-02T16:14:24+00:00","og_image":[{"url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png","type":"","width":"","height":""}],"author":"Martin Tilma","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Martin Tilma","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/","url":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/","name":"Customize namespace prefix when marshalling with Jaxb - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage"},"image":{"@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage"},"thumbnailUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png","datePublished":"2009-02-02T16:14:24+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/8b8a3958adbd9f58d1c257c622fe0f4e"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#primaryimage","url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png","contentUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png"},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/customize-namespace-prefix-when-marshalling-with-jaxb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Customize namespace prefix when marshalling with Jaxb"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/8b8a3958adbd9f58d1c257c622fe0f4e","name":"Martin Tilma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b4ccfbaec8216c8fa31c753299573cae?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b4ccfbaec8216c8fa31c753299573cae?s=96&d=mm&r=g","caption":"Martin Tilma"},"url":"https:\/\/trifork.nl\/blog\/author\/martin\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2484","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=2484"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2484\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=2484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=2484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=2484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}