以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  Element的toString方法不能解析出子节点的内容  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=17662)


--  作者:xiaoshuyu
--  发布时间:4/29/2005 10:09:00 AM

--  Element的toString方法不能解析出子节点的内容
我把Apache AXIS的jar包和xerces.jar加到classPath之后,Element的toString 方法就不能解析出内容来了,只得到了一个根节点的名字。
<学生花名册>
<学生 性别 = "男">
</学生>
</学生花名册>

这个Element,如果我调用它的toString方法,得到的是[学生花名册:null],而我想得到的是<学生花名册><学生 性别 = "男"></学生></学生花名册> 。帮个忙,急用,谢谢!


--  作者:xiaoshuyu
--  发布时间:4/30/2005 11:52:00 AM

--  


import java.io.*;
import java.util.*;
import javax.xml.parsers.*;

import org.apache.crimson.tree.XmlDocument;//
import org.xml.sax.*;
import org.w3c.dom.*;
public class Test{
 
 public Test(){
  
 }
 public InputSource parseXml(String inXml){
  if (inXml == null) {
  throw new IllegalArgumentException();
  }
  try {
  return  new InputSource(new StringReader(inXml));
  } catch (Exception e) {
  throw new RuntimeException(e.getMessage());
  }

 }
 
 public static void main(String[] args){
  
  Test tempTest = new Test();
  StringBuffer xmlString = new StringBuffer();
     xmlString.append("<?xml version=\"1.0\"?>\n");
     xmlString.append("<userAuth>\n");
     xmlString.append("    <token>").append("704e68fb6ccfc3e3f1b6d7ac41291c62").append("</token>\n");
     xmlString.append("    <username>").append("2509409").append("</username>\n");
     xmlString.append("    <serviceid>").append("001").append("</serviceid>\n");
     xmlString.append("    <contentid>").append("4").append("</contentid>\n");
     xmlString.append("    <contentname>").append("test").append("</contentname>\n");
     xmlString.append("    <price>").append("53").append("</price>\n");
     xmlString.append("    <police>").append("1").append("</police>\n");
     xmlString.append("    <ip>").append("192.168.0.2").append("</ip>\n");
     xmlString.append("    <spid>").append("sp001").append("</spid>\n");
        xmlString.append("</userAuth>");
  
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = null;
  try {
   db = dbf.newDocumentBuilder();
  } catch (ParserConfigurationException pce) {
   System.err.println(pce); //&sup3;&ouml;&Ograve;ì&sup3;&pound;&Ecirc;±&Ecirc;&auml;&sup3;&ouml;&Ograve;ì&sup3;&pound;&ETH;&Aring;&Iuml;&cent;&pound;&not;&Egrave;&raquo;&ordm;ó&Iacute;&Euml;&sup3;&ouml;&pound;&not;&Iuml;&Acirc;&Iacute;&not;
   System.exit(1);
  }
  
  Document doc = null;
  try {
   doc = db.parse(tempTest.parseXml(xmlString.toString()));
  } catch (Exception dom) {
   System.err.println(dom.getMessage());
   System.exit(1);
  }
  Element root = doc.getDocumentElement();
  System.out.println(root.toString());

 }

 }
 
 
}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms