Using the Java API Java to access metadata fields

I use the Rome API to parse data from an XML feed quite successfully, but I was a little mistaken for that.

Given the following XML fragment:

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

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

Getting a string representation of a feed entry would be an acceptable solution, since I could use string functions to extract information, but even with this I did not find a simple method.

Has anyone come into this?

Thanks.

+3
source share
3 answers

Here are the raw classes from our code without much explanation (but it's too late here!). This parses the 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 
+1
source

If you are not using it yet, v1.0RC1 has several parsing fixes. Maybe try updating?

0
source

I just couldn't get the modular approach to work. There was simply no module object. Either at the feed level or at the input level. Fortunately, the feed / entry.getForeignMarkup () method really worked for me.

0
source

All Articles