xml Feed - 自动处理 - 查找错误的网址

时间:2017-10-16 20:28:50

标签: xml

我有来自电子商务商店的xml产品Feed,有超过5000种产品。 Feed包含让我们说经典信息,如标题,描述,价格,图片,类别......

在某些地方可能有10个带URL地址的产品,图片无法加载(图片可能以某种方式错误地上传到产品中)。

我正在寻找能让我自动完成所有图像并检查图像是否已加载或未加载的解决方案(未加载的图像不会返回404页面)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我将从使用JAXB和ImageIO的Java中的以下代码开始。 JAXB解析XML和ImageIO以检查图像是否存在。我不确定你是否是一个Java人员,但同样的逻辑可能也适用于其他语言。

public static void main(String[] args) {

        File file = new File("C:\\DocuApi\\imageDemoApp\\src\\main\\resources\\static\\feed_shopy.xml");
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance("com.stack.image.dto");
            Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();

            System.out.println("Parsed XML successfully");

            List<String> imageList = new ArrayList<String>();
            List<String> nonImageList = new ArrayList<String>();


            SHOP shop = (SHOP)jaxbUnMarshaller.unmarshal(file);
            List<SHOPITEM> shopItems = shop.getSHOPITEM();
            for(SHOPITEM shopItem: shopItems){
                IMAGES images = shopItem.getIMAGES();
                List<String> imageURIs = images.getIMAGE();
                for(String imguri: imageURIs){
                    Image image;
                    try {
                        image = ImageIO.read(new URL(imguri));
                        if(image != null){

                            imageList.add(imguri);
                        }else{
                            nonImageList.add(imguri);
                        }
                    } catch (MalformedURLException e) {

                        nonImageList.add(imguri);
                        e.printStackTrace();
                    } catch (IOException e) {

                        nonImageList.add(imguri);
                        e.printStackTrace();
                    }
                }
            }

            System.out.println("Size of imageList is- " + imageList.size());
            System.out.println("Size of nonImageList is- " + nonImageList.size());
            for(String imageUrl: nonImageList){
                System.out.println("NonImage URL " + imageUrl);
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        } 

    }