---
title: "Default namespace with prefix for single XML element in JAVA"  
description: "Default namespace with prefix for single XML element in JAVA"  
author: "Anonymous User"  
published: 2014-11-05  
updated: 2014-11-06  
canonical: https://www.mindstick.com/forum/2524/default-namespace-with-prefix-for-single-xml-element-in-java  
category: "xml"  
tags: ["java"]  
reading_time: 1 minute  

---

# Default namespace with prefix for single XML element in JAVA

I want my [xml](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) element as follows

```
<exElement xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema">
```

I used the following [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)\

```
rootElement.setAttributeNS("urn:hl7-org:v3",
"xsd", "http://www.w3.org/2001/XMLSchema");
```

And gives me the Element as follows which is different than what i want.\

```
<exElement xmlns:ns0="urn:hl7-org:v3" xsi:ns1="http://www.w3.org/2001/XMLSchema">
```

Can [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) please correct my code if there is an issue? Help will be greatly appriciated.\

## Replies

### Reply by marcel ethan

Try this :

I am using xom library for XML-manipulation :

```
Element root = new Element("exElement");
root.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema");
root.setNamespaceURI("urn:hl7-org:v3");

Document document = new Document(root);
System.out.println("XML :: " + document.toXML());
```

that's working fine for me & give me result :

```
XML :: <?xml version="1.0"?>
<exElement xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema" />
```

still some problem post me.


---

Original Source: https://www.mindstick.com/forum/2524/default-namespace-with-prefix-for-single-xml-element-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
