XML文件:路径在 D:\\xml中 多个.xml结尾的文件
<root>
<pid>47ig84S_1371203166643_egnexsk</pid>
<channelid>47ig84S</channelid>
<title>test2</title>
<type>新闻节目</type>
<keyword>test2</keyword>
<intro />
<publishid>3</publishid>
<images />
<segments>
<item>1371139200000,YMZL,00:00:41,00:10:00</item>
<item>1371139200000,YOZE,-1,-1</item>
<item>1371139200000,YQZ9,-1,-1</item>
<item>1371139200000,YSZ6,00:00:00,00:00:41</item>
</segments>
</root>
dom4j解析多个本地XML获取节点:
package tidemedia.cms.chaitiao2;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test {
public static String folder ="D:\\xml";
public static String loc ="d:\\rec";
public static void main(String[] args) throws DocumentException {
getStringFileList();
}
public static String getStringFileList() throws DocumentException {
File f = new File(folder);
String temp = "";
if (f.isDirectory()) {
File[] ftemp = f.listFiles();
for (int i = 0; i < ftemp.length; i++) {
if ((ftemp[i].getName().endsWith("xml"))) {
if (temp.length() > 0) {
temp += ",";
}
temp += ftemp[i].getName();
}
}
}
System.out.println("temp xml文件名字数组-->"+temp);
String[] array = temp.split(",");
for(int i=0;i<array.length;i++){
String xmlpath = folder+"/"+array[i];
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(xmlpath));
Element root = doc.getRootElement();
Element segments = root.element("segments");
String channelid = root.elementText("channelid");
String str="";
for (Iterator<Element> j = segments.elementIterator("item");j.hasNext();)
{
Element foo = (Element) j.next();
cutd(foo,channelid,str);
//str += foo.getText();
//System.out.println(":"+foo.getText());
//System.out.println(foo.elementText("item"));
}
//System.out.println(channelid);
System.out.println(xmlpath);
//String source_file = target_path + "\\" + channelid + ".mp4";
//System.out.println(source_file);
}
//System.out.println("arr--->"+array);
return temp;
}
private static void cutd(Element foo, String channelid,String str) {
str = foo.getText();
String[] str2 = str.split(",");
String fullpath =loc+"\\"+channelid+"\\"+str2[0]+"\\"+str2[1]+".f4v";
System.out.println("fullpath-->"+fullpath);
}
}