免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2048 | 回复: 1
打印 上一主题 下一主题

请问为何这样访问会自动打印xml文件的内容? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-01 00:28 |只看该作者 |倒序浏览
使用jdom解析,访问一个网址得到的内容,会自动打印出来,如何才能不自动打印?jdom是怎样对一段文本进行解析的?

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

/**
* @author Administrator TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
*/
public class JDomTest
{

  public static void main(String args[])
  {
    try
    {

        org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
        //            创建文档
        String urlText =  getURL("http://cn.bbs.yahoo.com/rss/brss_kismet.html");
//        System.out.println(urlText+"aaa");
        org.jdom.Document doc = sb.build(urlText);

//        org.jdom.Document doc = sb.build(new FileInputStream("test.xml"));
        
        //            加入一条处理指令
//        org.jdom.ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", "href=\"bookList.html.xsl\" type=\"text/xsl\"");
        //            把这条处理指令,加入文档中
//        doc.addContent(pi);
        //            获得这个文档得跟元素
        org.jdom.Element el = doc.getRootElement();
        //            获得这个跟元素,的所有子元素
        java.util.List ls = el.getChildren();
        //            得到第一个子元素
        org.jdom.Element book = (Element) ls.get(1);
        //            给这个字元素添加一条属性
        
//        for(int i=0;i<ls.size();i++){
//          org.jdom.Element book2 = (Element) ls.get(i);
//          System.out.println(book2.getChildText("name")+" "+ book2.getChildText("price"));              
//        }

//        org.jdom.Attribute attr = new Attribute("hot", "true");
//        book.setAttribute(attr);
        //            System.out.println(book.getAttributeValue("hot"));

        //            获得这个元素的子元素,(指定)
//        org.jdom.Element elm2 = book.getChild("author");
        //            输出这个元素的值
//         System.out.println(elm2.getName());
//         System.out.println(elm2.getValue());
//        System.out.println();

        //            给这个元素的值改个名字
//        elm2.setText("测试苏联队发随asdf大流发斯蒂芬我们");
//        System.out.println(elm2.getValue());
        //            再指定元素获得这个值
//        org.jdom.Element el3 = book.getChild("price");
        //            给这个值换个值
//        el3.setText(Float.toString(50.0f));
//        String a = "";
//        boolean bool = true;

//        XMLOutputter xml = new org.jdom.output.XMLOutputter();
//        xml.output(doc, new FileOutputStream("e:\\a\\cute.xml"));

    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());

    }

  }

  public static String getURL(String s)
  {     
    BufferedReader br=null;
    URL url=null;
    URLConnection con;
    StringBuffer ret = new StringBuffer();
    DataInputStream dis;
    String sr="";
    try
      {
          url=new URL(s);
//           con = url.openConnection();
//           con.connect();
//           dis = new DataInputStream(con.getInputStream());
          br=new BufferedReader(new InputStreamReader(url.openStream(),"gbk"));//openStream()是为了获得一个inputstream
          String html="";
          while((html=br.readLine())!=null)
          {
            ret.append(html+"\r\n");
          }
          br.close();
          sr = new String(ret.toString().getBytes(),"GBK");
      }catch(Exception e)
      {
          e.printStackTrace();
      }      
      return sr;
   
  }
}

论坛徽章:
0
2 [报告]
发表于 2005-12-01 09:31 |只看该作者
org.jdom.Document doc = sb.build(urlText);
这句话好像有错误,然后System.out.println(e.getMessage());打印错误
没调试,猜的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP