2

我正在使用 Apache Commons Digester 并尝试在结构中定位特定标签以包含在对象中。

<parent>

<image size="small">some url</image>

<image size="medium">some url</image>

<image size="large">some url</image>

<image size="huge">some url</image>

</parent>

我真的只希望medium图像包含在我的partent对象中,但我不确定我会如何做到这一点。

现在我正在使用digester.addBeanPropertySetter(PathToParent+"/image","image");,但这会针对每个image标签进行更新(应该如此)。

理想情况下,我想要类似的东西,digester.addBeanPropertySetter(PathToParent+"/image/medium","image");但你不能那样做。

4

3 回答 3

2

我省略了通用的 getter/setter。

  public class Parent {

    private Image image;

    public void setImage(Image image) {
      if ("medium".equals(image.getSize())) {
        this.image = image;
      }
    }

  }

  public class Image {

    private String size;
    private String url;
  }

  public static void main(String[] args) throws IOException, SAXException {
    String s = "<parent>" 
        + "<image size='small'>some url1</image>"
        + "<image size='medium'>some url2</image>"
        + "<image size='large'>some url3</image>"
        + "<image size='huge'>some url4</image>"
        + "</parent>";

    Digester digester = new Digester();

    digester.addObjectCreate("parent", Parent.class);
    digester.addFactoryCreate("parent/image", new ImageCreationFactory());
    digester.addBeanPropertySetter("parent/image", "url");
    digester.addSetNext("parent/image", "setImage");
    Parent p = (Parent) digester.parse(new StringReader(s));
  }

  public class ImageCreationFactory implements ObjectCreationFactory {

    public Object createObject(Attributes attributes) throws Exception {
      Image i = new Image();
      i.setSize(attributes.getValue("size"));
      return i;
    }

  }
于 2010-12-05T17:53:32.537 回答
1

我实际上使用 xmlpullparser 解决了这个问题——这里是只获取图像属性“大”并忽略其余部分的代码——它是 case 语句中的最后一个“if”。

公共类 XmlPullFeedParser 扩展 BaseFeedParser {

public XmlPullFeedParser(String feedUrl) {
    super(feedUrl);
}

public ArrayList<Message> parse() {
    ArrayList<Message> messages = null;
    XmlPullParser parser = Xml.newPullParser();
    try {
        // auto-detect the encoding from the stream
        parser.setInput(this.getInputStream(), null);
        int eventType = parser.getEventType();
        Message currentMessage = null;
        boolean done = false;
        while (eventType != XmlPullParser.END_DOCUMENT && !done){
            String name = null;
            String attrib = null;
            switch (eventType){
                case XmlPullParser.START_DOCUMENT:
                    messages = new ArrayList<Message>();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    attrib = parser.getAttributeValue(0);
                    if (name.equalsIgnoreCase(EVENT)){
                        currentMessage = new Message();
                    } else if (currentMessage != null){
                        if (name.equalsIgnoreCase(WEBSITE)){
                            currentMessage.setWebsite(parser.nextText());
                        } else if (name.equalsIgnoreCase(DESCRIPTION)){
                            currentMessage.setDescription(parser.nextText());
                        } else if (name.equalsIgnoreCase(START_DATE)){
                            currentMessage.setDate(parser.nextText());
                        } else if (name.equalsIgnoreCase(TITLE)){
                            currentMessage.setTitle(parser.nextText());
                        } else if (name.equalsIgnoreCase(HEADLINER)){
                            currentMessage.setHeadliner(parser.nextText());
                        } else if ((name.equalsIgnoreCase(IMAGE)) && (attrib.equalsIgnoreCase("large"))) {
                            currentMessage.setImage(parser.nextText());
                        }   
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase(EVENT) && currentMessage != null){
                        messages.add(currentMessage);
                    } else if (name.equalsIgnoreCase(EVENTS)){
                        done = true;
                    }
                    break;
            }
            eventType = parser.next();
        }
    } catch (Exception e) {
        Log.e("AndroidNews::PullFeedParser", e.getMessage(), e);
        throw new RuntimeException(e);
    }
    return messages;
}

}

于 2011-02-24T12:24:10.477 回答
0

我不认为这是可能的。您必须编写自己的代码来执行这种过滤。

但这很简单。如果您希望使用方法创建名为 ImageAccessor 的干净代码编写类getImage(String size)。此方法将从消化器中获取数据并将其与预定义大小的字符串(或模式)进行比较。

于 2010-12-05T07:42:18.870 回答