当前位置: 技术问答>java相关
哪位有用Servlet通过DOM处理Xml的例子?
来源: 互联网 发布时间:2015-05-30
本文导语: 比如有个动态生成的XML页面: public void xmlPageOutput() { System.out.println(""); System.out.println(""); System.out.println("Bob"); System.out.println("20"); System.out.println(""); System.out.println(""); System...
比如有个动态生成的XML页面:
public void xmlPageOutput()
{
System.out.println("");
System.out.println("");
System.out.println("Bob");
System.out.println("20");
System.out.println("");
System.out.println("");
System.out.println("Tom");
System.out.println("19");
System.out.println("");
System.out.println("");
}
我想编一个DOM实现找到,然后判断它的值是不是Tom,如果是就把它的改为23(或者做类似处理也可以),然后再返给浏览器新的界面.
我不知道这里Servlet起到了一个什么作用,该怎么编写Servlet和DOM.
各位高手能不能传一个类似的例子?或者给指点几句,或者您有这方面的网站?谢谢了!!!
public void xmlPageOutput()
{
System.out.println("");
System.out.println("");
System.out.println("Bob");
System.out.println("20");
System.out.println("");
System.out.println("");
System.out.println("Tom");
System.out.println("19");
System.out.println("");
System.out.println("");
}
我想编一个DOM实现找到,然后判断它的值是不是Tom,如果是就把它的改为23(或者做类似处理也可以),然后再返给浏览器新的界面.
我不知道这里Servlet起到了一个什么作用,该怎么编写Servlet和DOM.
各位高手能不能传一个类似的例子?或者给指点几句,或者您有这方面的网站?谢谢了!!!
|
use another seperate class to parse (Transform) the XML file.
import org.w3c.dom.*;
// We are going to use JAXP's classes for DOM I/O
import java.io.*;
//import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
public class ParseProductXML {
Document myDoc;
public ParseProductXML(String xmlfile) {
try{
//XMLfile = filename;
File inputfile = new File(xmlfile);
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
myDoc = db.parse(inputfile);
}catch( ParserConfigurationException pcEx ){
System.out.println("ParserConfigurationException: "+pcEx.getMessage());
pcEx.printStackTrace(); //return false;
}//first try
}catch(Exception e) {
System.out.println("Parsing File failed: " + e.getMessage());
}//out try
}
public ArrayList getLabel(String parentname, String tagname){
int length = myDoc.getElementsByTagName(tagname).getLength();
ArrayList list = new ArrayList();
//System.out.println(myDoc.getElementsByTagName(tagname).item(0).getParentNode().getNodeName());
for(int i = 0; i
import org.w3c.dom.*;
// We are going to use JAXP's classes for DOM I/O
import java.io.*;
//import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
public class ParseProductXML {
Document myDoc;
public ParseProductXML(String xmlfile) {
try{
//XMLfile = filename;
File inputfile = new File(xmlfile);
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
myDoc = db.parse(inputfile);
}catch( ParserConfigurationException pcEx ){
System.out.println("ParserConfigurationException: "+pcEx.getMessage());
pcEx.printStackTrace(); //return false;
}//first try
}catch(Exception e) {
System.out.println("Parsing File failed: " + e.getMessage());
}//out try
}
public ArrayList getLabel(String parentname, String tagname){
int length = myDoc.getElementsByTagName(tagname).getLength();
ArrayList list = new ArrayList();
//System.out.println(myDoc.getElementsByTagName(tagname).item(0).getParentNode().getNodeName());
for(int i = 0; i