summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/utils/Dom4JElementParser.java
blob: a4d5bbed6db92c02432e2fafeb143adfae497c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.c2kernel.utils;

import java.io.StringReader;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4JElementParser
{
    public static String[] parse(String data, String xpath)
    {
        try
        {
        SAXReader reader = new SAXReader();
            
        Document d =  reader.read(new StringReader(data));
        List list = d.selectNodes( xpath );
        String[] returnArray = new String[list.size()];
        int i=0;
        for ( Iterator iter = list.iterator(); iter.hasNext();i++ ) 
        {    
           Object object = iter.next();             
           if (object instanceof Element) returnArray[i]=((Element)object).getText();
           else if (object instanceof Attribute) returnArray[i]=((Attribute)object).getText();
        }
        return returnArray;
        }
        catch (Exception e)
        {
            Logger.error(e);
            return new String[0];
        }
    }
}