1、自定义xml文件内容如下:
<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<doc>
<url>http://gongyi.sohu.com/20120706/n347457739.shtml</url>
<docno>98590b972ad2f0ea-34913306c0bb3300</docno>
<contenttitle>深圳地铁将设立VIP头等车厢 买双倍票可享坐票</contenttitle>
<content>南都讯 记者刘凡 周昌和 任笑一 继推出日票后,深圳今后将设地铁VI P头等车厢,设坐票制。昨日,《南都METRO》
</content>
</doc>
<doc>
<url>http://gongyi.sohu.com/20120724/n348878190.shtml</url>
<docno>5fa7926d2cd2f0ea-34913306c0bb3300</docno>
<contenttitle>爸爸为女儿百万建幼儿园 消防设施3年仍不过关</contenttitle>
<content></content>
</doc>
</RESULT>
2、用java语言解析上述的url和contenttitle,如果缺少相应的jar包,可以去我的博客免费下载,谢谢大家的支持
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class lianxi1 {
public static void main(String[] args) {
try {
File f = new File("上述xml文件的路径");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("doc");
for (int i = 0; i < nl.getLength(); i++) {
String path=doc.getElementsByTagName("url").item(i).getFirstChild().getNodeValue();
String contenttitle=doc.getElementsByTagName("contenttitle").item(i).getFirstChild().getNodeValue();
System.out.println("当前的路径为:"+path+" 内容标题为:"+contenttitle);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}