从网上找到一段解析html的代码,希望加上生成xml得功能,但不知为什么生成结果如下:<?xml version="1.0" encoding="GBK"?>
<list>
</list>if语句里面得内容没有输出来,希望高手指正。package html;
import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import java.io.*;
public class testnekohtml
{
public static void main(String[] argv) throws Exception
{
DOMParser parser = new DOMParser();
System.setProperty("http.proxyHost", "88.148.41.44");
System.setProperty("http.proxyPort", "51966");
parser.setFeature("http://xml.org/sax/features/namespaces", false);
// parser.parse("c:/aa.jsp");//也可以是如:http://www.searchfull.net/blog
//parser.parse("http://www.baidu.com");
parser.parse("d:/126.htm");
print(parser.getDocument(), "");
}
////////////////
public static void print(Node node, String indent)
{
try
{
// System.out.println(indent+node.getClass().getName());
// System.out.println(node.getNodeName());
File f = new File("d:/shit.xml");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("<?xml version=\"1.0\" encoding=\"GBK\"?>");
bw.newLine();
bw.write("<list>");
bw.newLine();
if ("INPUT".equals(node.getNodeName())||"input".equals(node.getNodeName())||"Input".equals(node.getNodeName()))
{
bw.write("<list>");
String name=node.getNodeName();
NamedNodeMap map=node.getAttributes();//获得标记中属性的集合
// String content=node.getTextContent();
//System.out.println(name);
for(int k=0;k<map.getLength();k++)
{
Attr attrNode=(Attr)map.item(k);
String attName=attrNode.getName();
String attValue=attrNode.getValue();
bw.write("<");
System.out.println("0");
bw.write(name+" ");
bw.write(" "+attName+"="+attValue);
bw.write(">");
bw.newLine();
//System.out.println(" "+attName+"="+attValue);
}//
// System.out.println(content);
}
Node child = node.getFirstChild();
while (child != null)
{
print(child, indent + " ");
child = child.getNextSibling();
}
bw.write("</list>");
bw.close();
fw.close();
}//try
catch(IOException e)
{
System.out.println("输出异常");
}
}
}//main
<list>
</list>if语句里面得内容没有输出来,希望高手指正。package html;
import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import java.io.*;
public class testnekohtml
{
public static void main(String[] argv) throws Exception
{
DOMParser parser = new DOMParser();
System.setProperty("http.proxyHost", "88.148.41.44");
System.setProperty("http.proxyPort", "51966");
parser.setFeature("http://xml.org/sax/features/namespaces", false);
// parser.parse("c:/aa.jsp");//也可以是如:http://www.searchfull.net/blog
//parser.parse("http://www.baidu.com");
parser.parse("d:/126.htm");
print(parser.getDocument(), "");
}
////////////////
public static void print(Node node, String indent)
{
try
{
// System.out.println(indent+node.getClass().getName());
// System.out.println(node.getNodeName());
File f = new File("d:/shit.xml");
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("<?xml version=\"1.0\" encoding=\"GBK\"?>");
bw.newLine();
bw.write("<list>");
bw.newLine();
if ("INPUT".equals(node.getNodeName())||"input".equals(node.getNodeName())||"Input".equals(node.getNodeName()))
{
bw.write("<list>");
String name=node.getNodeName();
NamedNodeMap map=node.getAttributes();//获得标记中属性的集合
// String content=node.getTextContent();
//System.out.println(name);
for(int k=0;k<map.getLength();k++)
{
Attr attrNode=(Attr)map.item(k);
String attName=attrNode.getName();
String attValue=attrNode.getValue();
bw.write("<");
System.out.println("0");
bw.write(name+" ");
bw.write(" "+attName+"="+attValue);
bw.write(">");
bw.newLine();
//System.out.println(" "+attName+"="+attValue);
}//
// System.out.println(content);
}
Node child = node.getFirstChild();
while (child != null)
{
print(child, indent + " ");
child = child.getNextSibling();
}
bw.write("</list>");
bw.close();
fw.close();
}//try
catch(IOException e)
{
System.out.println("输出异常");
}
}
}//main
解决方案 »
- 为什么我一用spring声明式事务`再去获得这个带事务的bean的话就会得到一个代理类啊
- Runtime.exec()与cmd或者sh的交互求教
- 点击页面上的一个链接,弹出两个页面是什么原因?
- 增加java定时器后,Tomcat 启动错误
- Struts+Spring的例子那为大哥能给我写一个小弟我不胜荣幸
- 如何将TOMCAT与IIS组合起来使用?
- 使用JavaBean连接DB2数据库时,出现错误
- 谁能告诉我EJB中Resource References如何配置,71分送上!!
- 求助,请问如何改写才能不重复爬取具有相同url和sitename的网址
- 路径不出错的情况下一直是servlet 404错误 该怎么办?
- JAVA的基本问题,很简单
- 一个非常奇怪的关于Struts2的问题
String attValue=attrNode.getValue();
bw.write(" <");
System.out.println("0");
bw.write(name+" ");
bw.write(" "+attName+"="+attValue);
bw.write(">");
bw.newLine();
是不是这块没值了,为空啊. 设断点调试下