From 254ee6f47eebfc00462c10756a92066e82cc1a96 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 21 Jun 2011 15:46:02 +0200 Subject: Initial commit --- source/com/c2kernel/utils/XmlElementParser.java | 115 ++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100755 source/com/c2kernel/utils/XmlElementParser.java (limited to 'source/com/c2kernel/utils/XmlElementParser.java') diff --git a/source/com/c2kernel/utils/XmlElementParser.java b/source/com/c2kernel/utils/XmlElementParser.java new file mode 100755 index 0000000..f6fef4b --- /dev/null +++ b/source/com/c2kernel/utils/XmlElementParser.java @@ -0,0 +1,115 @@ +package com.c2kernel.utils; + +import java.io.StringReader; +import java.util.StringTokenizer; +import java.util.Vector; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +public class XmlElementParser +{ + public static String[] parse(String data, String xpath) + { + try { + return Dom4JElementParser.parse(data, xpath); + } catch (NoClassDefFoundError ex) { + Logger.msg(5, "Using old xpath parser"); + return parseOld(data, xpath); + } + } + + public static String[] parseOld(String data, String path) + { + Vector returnData = new Vector(); + String[] returnArray = new String[0]; + try + { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + StringReader is = new StringReader(data); + Document doc = builder.parse(new InputSource(is)); + StringTokenizer pathTokens = new StringTokenizer(path, "/"); + int taille = pathTokens.countTokens(); + String[] pathElements = new String[taille]; + int i=taille; + while (pathTokens.hasMoreElements()) + pathElements[--i] = pathTokens.nextToken(); + + if (Logger.doLog(6)) { + Logger.msg(6, "Path elements:"); + for (int x=0;x