[jdom-interest] [SOLVED] Re: Using namespace prefixes for sub-elements

Lighton Phiri lighton.phiri at gmail.com
Thu Apr 12 04:57:45 PDT 2012


Hello,

Spoke too soon --using the 'addNamespaceDeclaration(Namespace arg0)'
method to the root element does exactly what I want.

// root element
Element rootNodeElement = new Element("resource");
rootNodeElement.addNamespaceDeclaration(dc);
rootNodeElement.addNamespaceDeclaration(dcterms);
rootNodeElement.addContent(dcTitle);
rootNodeElement.addContent(dctermsHasPart);
rootNodeElement.addContent(dctermsHasPart2);

-- Phiri


On 12 April 2012 13:13, Lighton Phiri <lighton.phiri at gmail.com> wrote:
> Hello,
>
> I've been going through the JavaDoc documentation and cannot seem to
> find anything that will help me create a document with fully qualified
> namespaces defined in the root element. I do not want to have to
> explicitly define the fully qualified namespace for each subsequent
> subelement that I define, but rather just use the namespace prefixes.
>
> Code
>
> // namespaces
> Namespace dc = Namespace.getNamespace("dc", "http://purl.org/dc/elements/1.1/");
> Namespace dcterms = Namespace.getNamespace("dcterms",
> "http://purl.org/dc/dcterms/");
>
> // title element
> Element dcTitle = new Element("title", dc);
> dcTitle.addContent("Notebooks");
>
> // hasPart element
> Element dctermsHasPart = new Element("hasPart", dcterms);
> dctermsHasPart.addContent("BC_151_A1_4_001");
> Element dctermsHasPart2 = new Element("hasPart", dcterms);
> dctermsHasPart2.addContent("BC_151_A1_4_002");
>
> // root element
> Element rootNodeElement = new Element("resource");
> rootNodeElement.addContent(dcTitle);
> rootNodeElement.addContent(dctermsHasPart);
> rootNodeElement.addContent(dctermsHasPart2);
>
> Output
>
> <?xml version="1.0" encoding="UTF-8"?>
> <resource>
>  <dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">Notebooks</dc:title>
>  <dcterms:hasPart
> xmlns:dcterms="http://purl.org/dc/dcterms/">A1_4_001</dcterms:hasPart>
>  <dcterms:hasPart
> xmlns:dcterms="http://purl.org/dc/dcterms/">A1_4_002</dcterms:hasPart>
> </resource>
>
> Desired output
>
> <?xml version="1.0" encoding="UTF-8"?>
> <resource xmlns:dc="http://purl.org/dc/elements/1.1/"
> xmlns:dcterms="http://purl.org/dc/dcterms/">
>  <dc:title>Notebooks</dc:title>
>  <dcterms:hasPart>A1_4_001</dcterms:hasPart>
>  <dcterms:hasPart>A1_4_002</dcterms:hasPart>
> </resource>
>
>
> -- Phiri



More information about the jdom-interest mailing list