com.love320.templateparser.label.impl.XMLToLabelImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.love320.templateparser.label.impl.XMLToLabelImpl.java

Source

/**
* Copyright (c) 2010-2012 love320.com
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* 
* Founder admin@love320.com
*/
package com.love320.templateparser.label.impl;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.love320.templateparser.factory.entity.Label;
import com.love320.templateparser.label.XMLToLabel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @ClassName: XMLToLabelImpl 
* @Description: TODO
* @author love320.com
* @date 2012-5-6 ?04:03:47 
*  
*/
public class XMLToLabelImpl implements XMLToLabel {

    private final static Logger logger = LoggerFactory.getLogger(XMLToLabelImpl.class);

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.XMLToLabel#get(java.lang.String)
     */
    @Override
    public List<Label> get(String xmlstr) {
        List<Label> labeList = new ArrayList<Label>();
        try {
            Document document = DocumentHelper.parseText(xmlstr);//string ?xml
            Element docroot = document.getRootElement();//?Element
            List ls = docroot.selectNodes("/root/content/item");//?item
            for (int i = 0; i < ls.size(); i++) {//??
                Element element = (Element) ls.get(i);//?
                String type = element.attributeValue("type");//?
                Label label = new Label();//
                label.setType(type);//
                label.setTemplate(element.getText());//
                labeList.add(label);//
                /*
                if(type.equals("label")){//
                   logger.info("label:"+element.getText());
                }else if(type.equals("symbols")){//?
                   logger.info("symbols:"+element.getText());
                }else{//
                   logger.info(element.getText());
                }
                */
            }
        } catch (DocumentException e) {
            logger.error("DocumentException", e);
        }
        return labeList;
    }

}