Hi all!

I have a problem that I've been struggling with for quite sometime. In short I've successfully parsed a xml file from a string. But I can't seem to access the correct elements. The thing is I'm reading the xml file data via javascript but in the xml file the nodes use namespaces. To get an element that doesn't have a namespace can I easily access.

In the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="pub-id" prefix="cc: http://creativecommons.org/ns#">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:title id="title">Moby-Dick</dc:title>
    <meta refines="#title" property="title-type">main</meta>
    <dc:creator id="creator">Herman Melville</dc:creator>
    <meta refines="#creator" property="file-as">MELVILLE, HERMAN</meta>
    <meta refines="#creator" property="role" scheme="marc:relators">aut</meta>
    <dc:identifier id="pub-id">code.google.com.epub-samples.moby-dick-basic</dc:identifier>
    <dc:language>en-US</dc:language>
    <meta property="dcterms:modified">2012-01-18T12:47:00Z</meta>
    <dc:publisher>Harper &amp; Brothers, Publishers</dc:publisher>
    <dc:contributor id="contrib1">Dave Cramer</dc:contributor>
    <meta refines="#contrib1" property="role" scheme="marc:relators">mrk</meta>
    <dc:rights>This work is shared with the public using the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.</dc:rights>        
    <link rel="cc:license" href="http://creativecommons.org/licenses/by-sa/3.0/"/>
    <meta property="cc:attributionURL">http://code.google.com/p/epub-samples/</meta>
  </metadata>

My javascript:

xmlDoc = parseXML(XMLString);

var a = xmlDoc.getElementById("creator");
var author = a.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","creator");

No this is what I've tried and it doens't seem to work. I did search for other options but couldn't find something that helped! If anyone could please help!

Member Avatar for LastMitch

In short I've successfully parsed a xml file from a string. But I can't seem to access the correct elements.

Does it work? Where did you get this XML format (another words how did you came up wiht this)?

<dc:title id="title">Moby-Dick</dc:title>

<meta refines="#title" property="title-type">main</meta>

<dc:creator id="creator">Herman Melville</dc:creator>

<meta refines="#creator" property="file-as">MELVILLE, HERMAN</meta>

<meta refines="#creator" property="role" scheme="marc:relators">aut</meta>

<dc:identifier id="pub-id">code.google.com.epub-samples.moby-dick-basic</dc:identifier>

<dc:language>en-US</dc:language>

<meta property="dcterms:modified">2012-01-18T12:47:00Z</meta>

<dc:publisher>Harper &amp; Brothers, Publishers</dc:publisher>

<dc:contributor id="contrib1">Dave Cramer</dc:contributor>

<meta refines="#contrib1" property="role" scheme="marc:relators">mrk</meta>

<dc:rights>This work is shared with the public using the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.</dc:rights> 

-

xmlDoc = parseXML(XMLString);
var a = xmlDoc.getElementById("creator");
var author = a.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","creator");

Instead of a shouldn't be title?

Try this:

var namespace = 'http://purl.org/dc/elements/1.1/';
var parser = new DOMParser(); 
var xml = parser.parseFromString(xml_file, "text/xml");    
var title = xml.getElementsByTagNameNS(namespace, 'title'); 
var title_value = title.getAttribute('creator');

Thanks for the replies!

The xml code is used as an e-book standard to display meta data. I did try your method but I seem to get 'undefined' and 'getAttribute()' doesn't exist error in FF version 15. So I tried this:

Using this in the ajax function below.

                               var title =  xmlDoc.getElementsByTagNameNS(namespace, 'creator');
                               alert(title);           //object HTMLCollection error (can't be an array)
                               alert(title.nodeValue); //undefined

And sometimes it may be like this in the xml @LastMitch <dc:title id="title">Moby-Dick</dc:title> so I can't find the value by attribute.

@pritaeas I actually did look at that forum aswell but it is a bit outdated and some code doens't work anymore on the new jquery. So after testing various cases this seemed to actually give me a solid output.

New Code:

function showMeta() {                 
                $.ajax({
                    type: "GET",
                    url: "boek/" + pathM,
                    dataType: "xml",
                    success: function(xml) {
                            $(xml).find('metadata').each(function(){
                                var meta = $(this).text();
                                alert(meta);
                            });
                    }
                });

So this outputs ALL the text in that metadata tag...I would like to have more control over it. To say like "Author: Herman Melville and Title: Moby-Dick". I've tried ALOT of different methods but non actually found the child nodes and it's text. Do you maybe have any advice on how to achieve this?

This really baffles me as this namespace makes it nearly impossible to find child elements.

I found a way which does the part - thanks for the help. Was just something small.

var title = $(this).find('dc\\:title').text();

I have the controll I wanted now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.