tags:

views:

933

answers:

2

I've been using the Rome API to parse data from an XML feed pretty successfully so for, but have run in to a bit of a snag.

Given the following snippet of XML:

<entry>
<id>uniqueId</id>
<updated>2008-11-05T01:32:35Z</updated>
<mm:status xmlns:mm="http://contentprovider.com&amp;quot; available="true"/>
<title>Title</title>
...
...
</entry>

Using the SyndEntryImpl class I'm able to use its standard methods (getTitle, getPublishedDate, etc) to pull the title, id, updated date, etc, but havent figured out a way to get the metadata tag (<mm:status ...).

Getting a string representation of the feed entry would be an acceptable solution as I'd be able to use string functions to retrieve the information, but even with that I havent found an easy method.

Has anyone run in to this in the past?

Thanks.

A: 

If you're not already using it, v1.0RC1 has several parsing fixes. Maybe try upgrading?

digitalsanctum
+1  A: 

Here is the raw classes for our code without much explanation (but it is late here!). This parses elements:

import com.sun.syndication.io.ModuleGenerator;
import com.sun.syndication.io.impl.DateParser;
import com.sun.syndication.feed.module.Module;

import java.util.Collections;
import java.util.Set;
import java.util.HashSet;

import org.jdom.Element;
import org.jdom.Namespace;

/**
 * Generates amplafi content in atom.
 */
public class AmplafiModuleGenerator implements ModuleGenerator {

    private static final Namespace NAMESPACE = Namespace.getNamespace("amplafi", AmplafiModule.URI);
    private static final Set<Namespace> NAMESPACES;

    static {
        Set<Namespace> namespaces = new HashSet<Namespace>();
        namespaces.add(NAMESPACE);
        NAMESPACES = Collections.unmodifiableSet(namespaces);
    }

    public String getNamespaceUri() {
        return AmplafiModule.URI;
    }

    public Set<Namespace> getNamespaces() {
        return NAMESPACES;
    }

    public void generate(Module module, Element element) {
        AmplafiModule myModule = (AmplafiModule) module;
        if (myModule.getStartDate() != null) {
            Element myElement = new Element("startDate", NAMESPACE);
            myElement.setText(DateParser.formatW3CDateTime(myModule.getStartDate()));
            element.addContent(myElement);
        }
        if (myModule.getEndDate() != null) {
            Element myElement = new Element("endDate", NAMESPACE);
            myElement.setText(DateParser.formatW3CDateTime(myModule.getEndDate()));
            element.addContent(myElement);
        }
    }
}

import com.sun.syndication.feed.module.Module;

import java.util.Date;

/**
 * Module for amplafi atom extension.
 */
public interface AmplafiModule extends Module {
    public static final String URI = "http://www.amplafi.com/namespace";

    public Date getStartDate();
    public void setStartDate(Date date);

    public Date getEndDate();
    public void setEndDate(Date date);
}

public class AmplafiModuleImpl extends ModuleImpl implements AmplafiModule {

    private Date startDate;
    private Date endDate;

    public AmplafiModuleImpl() {
        super(AmplafiModule.class, AmplafiModule.URI);
    }

    @Override
    public Class getInterface() {
        return AmplafiModule.class;
    }

    @Override
    public void copyFrom(Object obj) {
        AmplafiModule module = (AmplafiModule) obj;
        setStartDate(module.getStartDate());
        setEndDate(module.getEndDate());
    }

    @Override
    public Date getStartDate() {
        return startDate;
    }

    @Override
    public void setStartDate(Date date) {
        startDate = date;
    }

    @Override
    public Date getEndDate() {
        return endDate;
    }

    @Override
    public void setEndDate(Date date) {
        endDate = date;
    }

    @Override
    public String toString() {
        return "AmplafiModuleImpl{" +
                "startDate=" + startDate +
                ", endDate=" + endDate +
                '}';
    }
}

package com.amplafi.core.iomanagement.contentanalyzers.modules;

import java.util.Date;

import org.jdom.Element;
import org.jdom.Namespace;

import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser;
import com.sun.syndication.io.impl.DateParser;

/**
 * Parses amplafi content from atom.
 */
public class AmplafiModuleParser implements ModuleParser {

    public String getNamespaceUri() {
        return AmplafiModule.URI;
    }

    public Module parse(Element element) {
        Namespace myNamespace = Namespace.getNamespace(AmplafiModule.URI);
        AmplafiModule module = null;
        Date start = null;
        Date end = null;
        final Element startChild = element.getChild("startDate", myNamespace);
        if (startChild!=null) {
            start = DateParser.parseDate(startChild.getText());
        }
        final Element endChild = element.getChild("endDate", myNamespace);
        if (endChild!=null) {
            end = DateParser.parseDate(endChild.getText());
        }

        if (start!=null || end!=null) {
            module = new AmplafiModuleImpl();
            module.setStartDate(start);
            module.setEndDate(end);
        }
        return module;
    }
}

rome.properties:

atom_1.0.item.ModuleParser.classes=\
com.amplafi.core.iomanagement.contentanalyzers.modules.AmplafiModuleParser

atom_1.0.item.ModuleGenerator.classes=\
com.amplafi.core.iomanagement.contentanalyzers.modules.AmplafiModuleGenerator
Pat