자바 재 귀적 XML 모든 요소 옮 겨 다 니 기
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;
/**
 * Java    XML    
 * 
 * @author  Administrator
 * @version  [   , Apr 13, 2010]
 * @see  [   /  ]
 * @since  [  /    ]
 */
public class XmlParser
{
    // private static Map xmlmap = new HashMap(); 
    //  xml        
    private static ArrayList elemList = new ArrayList();
    
    public static void main(String args[])
        throws DocumentException
    {
        XmlParser test = new XmlParser();
        String path = "C:/a.xml";
        //   XML  
        SAXReader reader = new SAXReader();
        Document doc = reader.read(path);
        //   XML   
        Element root = doc.getRootElement();
        test.getElementList(root);
        String x = test.getListString(elemList);
        System.out.println("-----------    ------------
" + x);
    }
    
    /** 
     *          
     * 
     * @param element
     * @return
     * @see [ 、 #  、 #  ]
     */
    public String getNoteAttribute(Element element)
    {
        String xattribute = "";
        DefaultAttribute e = null;
        List list = element.attributes();
        for (int i = 0; i < list.size(); i++)
        {
            e = (DefaultAttribute)list.get(i);
            //System.out.println("name = " + e.getName() + ", value = " + e.getText());
            xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";
        }
        return xattribute;
    }
    
    /** 
     *        
     * 
     * @param element
     * @see [ 、 #  、 #  ]
     */
    public void getElementList(Element element)
    {
        List elements = element.elements();
        //       
        if (elements.isEmpty())
        {
            String xpath = element.getPath();
            String value = element.getTextTrim();
            elemList.add(new Leaf(getNoteAttribute(element), xpath, value));
        }
        else
        {
            //     
            Iterator it = elements.iterator();
            while (it.hasNext())
            {
                Element elem = (Element)it.next();
                //      
                getElementList(elem);
            }
        }
    }
    
    public String getListString(List elemList)
    {
        StringBuffer sb = new StringBuffer();
        for (Iterator it = elemList.iterator(); it.hasNext();)
        {
            Leaf leaf = (Leaf)it.next();
            sb.append("xpath: " + leaf.getXpath()).append(", value: ").append(leaf.getValue());
            if (!"".equals(leaf.getXattribute()))
            {
                sb.append(", Attribute: ").append(leaf.getXattribute());
            }
            sb.append("
");
        }
        return sb.toString();
    }
}
/** 
* xml       
*/
class Leaf
{
    //     
    private String xattribute;
    
    //   PATH
    private String xpath;
    
    //    
    private String value;
    
    public Leaf(String xattribute, String xpath, String value)
    {
        this.xattribute = xattribute;
        this.xpath = xpath;
        this.value = value;
    }
    
    public String getXpath()
    {
        return xpath;
    }
    
    public void setXpath(String xpath)
    {
        this.xpath = xpath;
    }
    
    public String getValue()
    {
        return value;
    }
    
    public void setValue(String value)
    {
        this.value = value;
    }
    
    public String getXattribute()
    {
        return xattribute;
    }
    
    public void setXattribute(String xattribute)
    {
        this.xattribute = xattribute;
    }
}
  이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.