您的位置:程序门 -> java -> web services / xml



取xml节点下的值的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


取xml节点下的值的问题
发表于:2007-12-19 15:57:28 楼主
正面的xml,java中用什么函数能取出字符串"test"   来?    
<?xml   version="1.0"   encoding="utf-8"   standalone="no"?>            
<学生   性别="aa">
          <姓名> 李华 </姓名>
          <年龄> 14 </年龄>
          <电话> 6287555 </电话>
          test
</学生>


node.getnodevalue();   //得到null
node.gettextcontent();   //得到   "李华146287555"   并不能取得"test",   现在我想单独取出   <学生>   下的   "test"   值.  
请指教。
发表于:2007-12-20 02:53:551楼 得分:0
jscript code
function selectnodes(xmlnode,sxpath){ //如果浏览器是ie if(window.activexobject){ //使用ie的xpath方式获得节点 return xmlnode.selectnodes(sxpath); } else{ //firefox类浏览器的处理方式 var oevaluator = new xpathevaluator(); if(oevaluator != null){ var oresult = oevaluator.evaluate(sxpath,xmlnode,null, xpathresult.ordered_node_iterator_type, null); var nodes = new array(); var node; while(node=oresult.iteratenext()){ nodes.push(node); } return nodes; } else{ return null; } }
<br/>
jscript code
function loadxml(flag, xml) { var xmldoc; //针对ie浏览器 if (window.activexobject) { var aversions = ["msxml2.domdocument.6.0","msxml2.domdocument.5.0", "msxml2.domdocument.4.0","msxml2.domdocument.3.0", "msxml2.domdocument","microsoft.xmldom"]; forvar i = 0; i < aversions.length; i++) { try { //建立xml对象 xmldoc = new activexobject(aversions[i]); break; } catch (oerror) { } } if (xmldoc != null) { //同步方式加载xml数据 xmldoc.async = false; //根据xml文档名称装载 if (flag == true) { xmldoc.load(xml); } else { //根据表示xml文档的字符串装载 xmldoc.loadxml(xml); } //返回xml文档的根元素节点。 //xmldoc现在已经表示一个dom树的根节点 //如果需要返回根节点,可以写成 return xmldoc return xmldoc.documentelement; } } else { //针对非ie浏览器 if (document.implementation && document.implementation.createdocument) { /* 第一个参数表示xml文档使用的namespace的url地址 第二个参数表示要被建立的xml文档的根节点名称 第三个参数是一个doctype类型对象,表示的是要建立的xml文档中doctype部分的定义,通常我们直接使用null 这里我们要装载一个已有的xml文档,所以首先建立一个空文档,因此使用下面的方式 */ xmldoc = document.implementation.createdocument("", "", null); if (xmldoc != null) { //根据xml文档名称装载 if (flag == true) { //同步方式加载xml数据,和ie的方式相同 xmldoc.async = false; xmldoc.load(xml); } else { //根据表示xml文档的字符串装载 var oparser = new domparser(); // parsefromstring的第一个参数就是xml的字符串,第二个参数固定是"text/xml" xmldoc = oparser.parsefromstring(xml, "text/xml"); } //返回xml文档的根元素节点。 return xmldoc.documentelement; } } } return null; }
</br>
jscript code
var xmldoc = loadxml(true,"[color=#ff0000]yourxml[/color].xml" var nametext = selectnodes(xmldoc,"//学生/text()"););
<br/>
取nametext的vaule值
发表于:2007-12-20 02:58:192楼 得分:0
用的是javascript,做个参考
发表于:2007-12-20 11:40:103楼 得分:0
import   java.io.file;

import   org.dom4j.document;
import   org.dom4j.element;
import   org.dom4j.io.saxreader;

public   class   parsexml   {

public   string   getxmlvalue(string   filename)throws   exception{
document   doc   =   null;  
        saxreader   reader   =   new   saxreader();
doc   =   reader.read(new   file(filename));
element   root   =   doc.getrootelement();
return   root.gettexttrim().tostring();
}

public   static   void   main(string   args[])   throws   exception{
string   filename   =   "c:\\aa.xml";
parsexml   parsexml   =   new   parsexml();
system.out.println(parsexml.getxmlvalue(filename));
}
}
发表于:2007-12-20 11:41:214楼 得分:0
aa.xml文件内容

<?xml       version="1.0"       encoding="gb2312"       standalone="no"?>                        
<学生       性别="aa">
                    <姓名>   李华   </姓名>
                    <年龄>   14   </年龄>
                    <电话>   6287555   </电话>
                    test
</学生>
发表于:2007-12-20 12:26:105楼 得分:0
jdom
发表于:2007-12-20 13:11:056楼 得分:0
import   javax.xml.parsers.documentbuilder;
import   javax.xml.parsers.documentbuilderfactory;

import   org.w3c.dom.document;
import   org.w3c.dom.namednodemap;
import   org.w3c.dom.node;
import   org.w3c.dom.nodelist;

public   class   documenttest   {
public   static   document   createdom(string   filepath)   {
document   document   =   null;
try   {
documentbuilderfactory   dbf   =   documentbuilderfactory.newinstance();
documentbuilder   builder   =   dbf.newdocumentbuilder();
document   =   builder.parse(filepath);
}   catch   (exception   e)   {
//   logger.error(e.getmessage());
}
return   document;
}
public   void   test(document   document){
node   firstnode   =   document.getfirstchild();
string   text   =   firstnode.gettextcontent();
system.out.println("text:   "+text);
nodelist   nodelist   =   firstnode.getchildnodes();
for(int   i   =   0     ;   i   <   nodelist.getlength();   i++){
node   childnode   =   nodelist.item(i);
if(childnode.getnodetype()==3){
string   nodevalue   =childnode.getnodevalue().trim();
if(!"".equals(nodevalue)){
system.out.println("nodevalue:   "+nodevalue);
}
}
}
}

public   static   void   main(string   ss[]){
string   filepath   =   "f:\\test\\test.xml";
documenttest   dt   =   new   documenttest();
document   document   =   dt.createdom(filepath);
dt.test(document);
}
}


快速检索

最新资讯
热门点击