[jdom-interest] Using namespace prefixes for sub-elements

Lighton Phiri lighton.phiri at gmail.com
Thu Apr 12 05:29:31 PDT 2012


Hello Rolf,

Thank you very much.

Lighton Phiri
http://lightonphiri.org
http://en.gravatar.com/lightonphiri
Twitter: @lightonphiri


On 12/04/2012 14:10, Rolf Lear wrote:
> Hi Phiri.
>
> 'declare' the namespaces at the root level:
>
> http://hunterhacker.github.com/jdom/jdom2/apidocs/org/jdom2/Element.html#addNamespaceDeclaration(org.jdom2.Namespace)
>
> rootNodeElement.addNamespaceDeclaration(dc);
> rootNodeElement.addNamespaceDeclaration(dcterms);
>
>
> Rolf
>
>
> On Thu, 12 Apr 2012 13:13:38 +0200, 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
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


More information about the jdom-interest mailing list