<?xml version="1.0" encoding="utf-8"?>
<PDA >
<title title="PDA"></title>
<formid formid="0"></formid>
<first>
<title title="信息采集系统"></title>
<formid formid="01"></formid>
<tableName tableName="商品"></tableName>
<id id="1"></id>
<type type="name1"></type>
<name name="数据采集"></name>
</first>
<seconed>
<title title="用户信息"></title>
<formid formid="0101"></formid>
<tableName tableName="product"></tableName>
<id id="101"></id>
<type type="button1"></type>
<name name="取消"></name>
<fieldname fieldname=""></fieldname>
</seconed>
<seconed>
<title title="帐号管理"></title>
<formid formid="0102"></formid>
<tableName tableName="product"></tableName>
<id id="102"></id>
<type type="buttonno"></type>
<name name="确定"></name>
<fieldname fieldname=""></fieldname>
</seconed>
<first>
<title title="发布"></title>
<formid formid="02"></formid>
<tableName tableName=""></tableName>
<id id="2"></id>
<type type=""></type>
<name name="发布"></name>
</first>
<first>
<title title="二维码"></title>
<formid formid="03"></formid>
<tableName tableName=""></tableName>
<id id="3"></id>
<type type=""></type>
<name name="用户管理"></name>
</first>
<first>
<title title="04"></title>
<formid formid="04"></formid>
<tableName tableName=""></tableName>
<id id="4"></id>
<type type=""></type>
<name name="图片采集"></name>
</first>
</PDA>
4 个解决方案
#1
xpath //title/@title
#2
不懂,帮顶啊。
#3
用到dtree 详细参见http://jidalyg-8711.iteye.com/blog/509568
unction selectCategory(filePath)
{
if(filePath == 0)
{
$(”#dtree”).html(”);
return;
}
$.ajax({
type:”GET”,
url:filePath+”?”+Math.random(),
dataType:”xml”,
data:”",
success:function(data){
categoryContent = data;
//alert(data);
generateTree();
}
});
}
//生成树状结构
function generateTree()
{
index_id = 0;
//d必须是全局变量
d = new dTree(”d”);
d.add(index_id,-1,”分类体系”);
index_id++;
dfs(d,0,$(categoryContent).find(”tree”));
$(”#dtree”).html(d.toString());
}
//遍历xml文件
function dfs(d,parentId,node)
{
if(node == undefined)return;
$(node).children().each(function(i){
d.add(index_id,parentId,$(this).attr(”text”)+’('+$(this).attr(”id”)+’)',”javascript:choose(’”+$(this).attr(”text”)+’_'+$(this).attr(”id”)+”‘)”);
index_id++;
dfs(d,index_id-1,this);
})
}
unction selectCategory(filePath)
{
if(filePath == 0)
{
$(”#dtree”).html(”);
return;
}
$.ajax({
type:”GET”,
url:filePath+”?”+Math.random(),
dataType:”xml”,
data:”",
success:function(data){
categoryContent = data;
//alert(data);
generateTree();
}
});
}
//生成树状结构
function generateTree()
{
index_id = 0;
//d必须是全局变量
d = new dTree(”d”);
d.add(index_id,-1,”分类体系”);
index_id++;
dfs(d,0,$(categoryContent).find(”tree”));
$(”#dtree”).html(d.toString());
}
//遍历xml文件
function dfs(d,parentId,node)
{
if(node == undefined)return;
$(node).children().each(function(i){
d.add(index_id,parentId,$(this).attr(”text”)+’('+$(this).attr(”id”)+’)',”javascript:choose(’”+$(this).attr(”text”)+’_'+$(this).attr(”id”)+”‘)”);
index_id++;
dfs(d,index_id-1,this);
})
}
#4
动态还是用Xpath吧,事例代码如下
package com.test;
import java.text.ParseException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
public class parseXML {
public static void XpathParse(String target) throws ParseException {
Document doc = null;
try {
doc = DocumentHelper.parseText(target);
} catch (DocumentException e) {
e.printStackTrace();
}
Node pda= doc.selectSingleNode("//PDA"); //单个Node
System.out.println(pda.getName());
List<Node> childNodes1 = doc.selectNodes("//first/title");
List<Node> childNodes2 = doc.selectNodes("//first/title/@title");
for (Node obj : childNodes1) //找title的List<Node>
{
System.out.println(obj.getName());
}
for (Node obj : childNodes2) //找title里title属性
{
System.out.println(obj.getText());
}
// System.out.println(obj.getText(("//title/@title")));
// List<Node> childNodes = doc.selectNodes("//second");
}
public static void main(String[] args) throws Exception {
String target = "<?xml version='1.0' encoding='utf-8'?>"
+ " <PDA > <title title='PDA'></title> <formid formid='0'></formid> "
+ " <first> <title title='信息采集系统'></title>"
+ " <formid formid='01'></formid> <tableName tableName='商品'> </tableName> <id id='1'></id>"
+ " <type type='name1'></type> <name name='数据采集'></name> </first>"
+ " <second> <title title='用户信息'> </title> <formid formid='0101'></formid>"
+ " <tableName tableName='product'> </tableName> <id id='101'></id> <type type='button1'></type>"
+ " <name name='取消'></name> <fieldname fieldname=''></fieldname> </second>"
+ " <second> <title title='帐号管理'> </title> <formid formid='0102'></formid> "
+ " <tableName tableName='product'></tableName> <id id='102'></id> <type type='buttonno'></type>"
+ " <name name='确定'></name> <fieldname fieldname=''></fieldname> </second>"
+ " <first> <title title='发布'></title> <formid formid='02'></formid> "
+ " <tableName tableName=''></tableName> <id id='2'></id> <type type=''></type>"
+ " <name name='发布'></name> </first>"
+ " <first> <title title='二维码'></title> <formid formid='03'></formid> "
+ " <tableName tableName=''></tableName> <id id='3'></id> <type type=''></type>"
+ " <name name='用户管理'></name> </first>"
+ " <first> <title title='04'></title> <formid formid='04'></formid> "
+ " <tableName tableName=''></tableName> <id id='4'></id> <type type=''></type>"
+ " <name name='图片采集'> </name> </first>" + "</PDA>";
XpathParse(target);
}
}
#1
xpath //title/@title
#2
不懂,帮顶啊。
#3
用到dtree 详细参见http://jidalyg-8711.iteye.com/blog/509568
unction selectCategory(filePath)
{
if(filePath == 0)
{
$(”#dtree”).html(”);
return;
}
$.ajax({
type:”GET”,
url:filePath+”?”+Math.random(),
dataType:”xml”,
data:”",
success:function(data){
categoryContent = data;
//alert(data);
generateTree();
}
});
}
//生成树状结构
function generateTree()
{
index_id = 0;
//d必须是全局变量
d = new dTree(”d”);
d.add(index_id,-1,”分类体系”);
index_id++;
dfs(d,0,$(categoryContent).find(”tree”));
$(”#dtree”).html(d.toString());
}
//遍历xml文件
function dfs(d,parentId,node)
{
if(node == undefined)return;
$(node).children().each(function(i){
d.add(index_id,parentId,$(this).attr(”text”)+’('+$(this).attr(”id”)+’)',”javascript:choose(’”+$(this).attr(”text”)+’_'+$(this).attr(”id”)+”‘)”);
index_id++;
dfs(d,index_id-1,this);
})
}
unction selectCategory(filePath)
{
if(filePath == 0)
{
$(”#dtree”).html(”);
return;
}
$.ajax({
type:”GET”,
url:filePath+”?”+Math.random(),
dataType:”xml”,
data:”",
success:function(data){
categoryContent = data;
//alert(data);
generateTree();
}
});
}
//生成树状结构
function generateTree()
{
index_id = 0;
//d必须是全局变量
d = new dTree(”d”);
d.add(index_id,-1,”分类体系”);
index_id++;
dfs(d,0,$(categoryContent).find(”tree”));
$(”#dtree”).html(d.toString());
}
//遍历xml文件
function dfs(d,parentId,node)
{
if(node == undefined)return;
$(node).children().each(function(i){
d.add(index_id,parentId,$(this).attr(”text”)+’('+$(this).attr(”id”)+’)',”javascript:choose(’”+$(this).attr(”text”)+’_'+$(this).attr(”id”)+”‘)”);
index_id++;
dfs(d,index_id-1,this);
})
}
#4
动态还是用Xpath吧,事例代码如下
package com.test;
import java.text.ParseException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
public class parseXML {
public static void XpathParse(String target) throws ParseException {
Document doc = null;
try {
doc = DocumentHelper.parseText(target);
} catch (DocumentException e) {
e.printStackTrace();
}
Node pda= doc.selectSingleNode("//PDA"); //单个Node
System.out.println(pda.getName());
List<Node> childNodes1 = doc.selectNodes("//first/title");
List<Node> childNodes2 = doc.selectNodes("//first/title/@title");
for (Node obj : childNodes1) //找title的List<Node>
{
System.out.println(obj.getName());
}
for (Node obj : childNodes2) //找title里title属性
{
System.out.println(obj.getText());
}
// System.out.println(obj.getText(("//title/@title")));
// List<Node> childNodes = doc.selectNodes("//second");
}
public static void main(String[] args) throws Exception {
String target = "<?xml version='1.0' encoding='utf-8'?>"
+ " <PDA > <title title='PDA'></title> <formid formid='0'></formid> "
+ " <first> <title title='信息采集系统'></title>"
+ " <formid formid='01'></formid> <tableName tableName='商品'> </tableName> <id id='1'></id>"
+ " <type type='name1'></type> <name name='数据采集'></name> </first>"
+ " <second> <title title='用户信息'> </title> <formid formid='0101'></formid>"
+ " <tableName tableName='product'> </tableName> <id id='101'></id> <type type='button1'></type>"
+ " <name name='取消'></name> <fieldname fieldname=''></fieldname> </second>"
+ " <second> <title title='帐号管理'> </title> <formid formid='0102'></formid> "
+ " <tableName tableName='product'></tableName> <id id='102'></id> <type type='buttonno'></type>"
+ " <name name='确定'></name> <fieldname fieldname=''></fieldname> </second>"
+ " <first> <title title='发布'></title> <formid formid='02'></formid> "
+ " <tableName tableName=''></tableName> <id id='2'></id> <type type=''></type>"
+ " <name name='发布'></name> </first>"
+ " <first> <title title='二维码'></title> <formid formid='03'></formid> "
+ " <tableName tableName=''></tableName> <id id='3'></id> <type type=''></type>"
+ " <name name='用户管理'></name> </first>"
+ " <first> <title title='04'></title> <formid formid='04'></formid> "
+ " <tableName tableName=''></tableName> <id id='4'></id> <type type=''></type>"
+ " <name name='图片采集'> </name> </first>" + "</PDA>";
XpathParse(target);
}
}