定位 Digester 中的特定属性 - Java

发布于 2024-10-05 22:32:40 字数 632 浏览 7 评论 0原文

我正在使用 Apache Commons Digester 并尝试在结构中找到要包含在对象中的特定标记。

<父级>

<图片大小=“小”>某个网址

<图片大小=“中”>某个网址

<图片大小=“大”>某个网址

<图片大小=“巨大”>某个网址

我真的只想将 medium 图像包含在我的 partent 对象中,但我不知道如何我会那么做。

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

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

I'm using the Apache Commons Digester and trying to locate a particular tag in the structure to include in the object.

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

I really only want the medium image to be included in my partent object but I'm not sure how I would do that.

Right now I'm using digester.addBeanPropertySetter(PathToParent+"/image","image"); but this gets updated for every image tag (as it should).

Ideally I would like something like digester.addBeanPropertySetter(PathToParent+"/image/medium","image"); but you can't do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-10-12 22:32:40

我省略了通用的 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;
    }

  }

I omitted generic getters/setters.

  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;
    }

  }
平定天下 2024-10-12 22:32:40

我实际上使用 xmlpullparser 解决了这个问题 - 这是仅获取图像属性“large”并忽略其余部分的代码 - 这是 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;
}

}

I actually figured this out using the xmlpullparser - here is the code to get the image attribute "large" only and ignore the rest - it's the last "if" in the case statement.

public class XmlPullFeedParser extends 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;
}

}

听不够的曲调 2024-10-12 22:32:40

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

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

I do not think that it is possible. You have to write your own code to perform this kind of filtering.

But it is very simple. If you wish to create clean code write class named ImageAccessor with method getImage(String size). This method will get the data from digester and compare it with predefined size string (or pattern).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文