Java获取网页源码处理

来源:互联网 发布:c51单片机的最小系统图 编辑:程序博客网 时间:2024/06/05 03:53




对象方法

document.write()                  // 动态向页面写入内容

document.createElement_x(Tag)       // 创建一个html标签对象

document.getElementByIdx_x(ID)       // 获得指定ID值的对象

document.getElementsByName(Name)  // 获得指定Name值的对象




===============================================================================================================

试试,不过个人还是推荐使用dom4J,简单,速度快,dom4j+xpath方式解XML非常直观,比用Dom方式好很多。

Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static String XMLtoStr(Document document) {
        String result = null;
 
        if (document != null) {
            StringWriter strWtr = new StringWriter();
            StreamResult strResult = new StreamResult(strWtr);
            TransformerFactory tfac = TransformerFactory.newInstance();
            try {
                Transformer t = tfac.newTransformer();
                t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
                t.setOutputProperty(OutputKeys.INDENT, "yes");
                t.setOutputProperty(OutputKeys.METHOD, "xml");
                t.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount""4");
                t.transform(new DOMSource(document.getDocumentElement()),
                        strResult);
            catch (Exception e) {
                System.err.println("XML.toString(Document): " + e);
            }
            result = strResult.getWriter().toString();
        }
 
        return result;
    }

==================================================================================================