Using the Rome Java API to access metadata fields

馋奶兔 提交于 2021-02-07 04:06:25

问题


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&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.


回答1:


Here is the raw classes from 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 mp content in atom.
 */
public class ModuleGenerator implements ModuleGenerator {

    private static final Namespace NAMESPACE = Namespace.getNamespace("mp", Module.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 Module.URI;
    }

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

    public void generate(Module module, Element element) {
        Module myModule = (Module) 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 mp atom extension.
 */
public interface Module extends Module {
    public static final String URI = "http://www.mp.com/namespace";

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

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

public class ModuleImpl extends ModuleImpl implements Module {

    private Date startDate;
    private Date endDate;

    public ModuleImpl() {
        super(Module.class, Module.URI);
    }

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

    @Override
    public void copyFrom(Object obj) {
        Module module = (Module) 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 "ModuleImpl{" +
                "startDate=" + startDate +
                ", endDate=" + endDate +
                '}';
    }
}

package com.mp.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 mp content from atom.
 */
public class ModuleParser implements ModuleParser {

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

    public Module parse(Element element) {
        Namespace myNamespace = Namespace.getNamespace(Module.URI);
        Module 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 ModuleImpl();
            module.setStartDate(start);
            module.setEndDate(end);
        }
        return module;
    }
}

rome.properties:

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

atom_1.0.item.ModuleGenerator.classes=\
com.mp.core.iomanagement.contentanalyzers.modules.ModuleGenerator



回答2:


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




回答3:


I just couldn't make the module approach work. Simply the module object wasn't there. Either at the feed or entry level. Luckily the method feed/entry .getForeignMarkup() did work for me.



来源:https://stackoverflow.com/questions/342619/using-the-rome-java-api-to-access-metadata-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!