www.pudn.com > xmadddell.rar > ParseXML.java


//不可曾于 20050525  
import javax.servlet.*;  
import javax.servlet.http.*;  
import java.io.*;  
import java.util.*;  
import java.net.*;  
import java.sql.*; 
import javax.sql.*;  
import java.text.*;  
//import oracle.jdbc.driver.*;  
import javax.naming.*;  
 
public class ParseXML extends HttpServlet { 
 
	private String rootPath; 
	  
	public void init(ServletConfig config) throws ServletException 
	{ 	 
		ServletContext servletContext = config.getServletContext();  
		 rootPath = servletContext.getRealPath("/")   ;  
  
	  
	}  
	//13366081786 
class XMLNode 
{  
	public XMLNode() 
	{  
 
	} 
	public  String nodenum[];  
	public   String barg[]; 
	public  String earg[];  
	public XMLNode xmlnode[]; 
}; 
	public XMLNode  ParseXMLToNode(String nodenum,String src) 
	{ 
	  
			 //构造nodenum  
			 XMLNode xmlnode = new XMLNode()   ; 
			//System.out.println("ParseXMLToNode="+src); 
			//System.out.println("########################################################"); 
			 int thiscount = 0; 
			 
	  
			 StringTokenizer  ASection   = new StringTokenizer(src, ">"); 
			 if(ASection.countTokens()==0) return null; 
			  
			 String arg[] = src.split(">"); 
 
			 String argnum[] = new String[arg.length]; 
			 String barg[] = new String[arg.length]; 
			 String earg[] = new String[arg.length]; 
			  XMLNode xmlnodechild[] = new XMLNode[arg.length]; 
			 int nlen = arg.length; 
			 for( int i=0;i"; 
			} 
			 int i=0; 
			  while( i"); 
					  sfind = sfind[0].split("<"); 
						  
								  
							   
					  if(sfind.length>1) 
					  { 
						   
						  
						  sfind[1] = sfind[1].trim();    
						  int nff = sfind[1].indexOf(" "); 
						  if(nff>=0)sfind[1] = sfind[1].substring(0,nff); 
						 // System.out.println("sfind="+sfind[1]);  
					 
						  int nshouldfind = 0; 
						  int nhavefind = 0; 
						  for(int j=i;j")  
								 ) nshouldfind++; 
							 if(arg[j].indexOf("/"+sfind[1]+">")!=-1) nhavefind++; 
 
					 
							   
							  if(nshouldfind==nhavefind) 
							  {  
									 
								  barg[thiscount] = arg[i]; 
								  earg[thiscount] = arg[j]; 
								  argnum[thiscount] = nodenum+"."+String.valueOf(thiscount); 
						  
								   
								//  System.out.println(arg[i]+":if(nshouldfind==nhavefind)nshouldfind="+nshouldfind); 
								    
								 
								  if(i!=j) 
								  { 
									  
									  String newsrc = ""; 
									  for(int k=i+1;k1) 
 
				  } 
 
			   i++; 
	  
			 }//for(int i=0;i "); 
 
			try 
			{ 
				//IniFile inifile = new IniFile();	 
				//String sections = inifile.getSectionString(rootPath+"user.xml", "Main" ) ; 
				 BufferedReader bufferedReader = new BufferedReader(new FileReader(rootPath+"user.xml")); 
				 String allLine = ""; 
				 String oneLine = ""; 
				  
				  while ((oneLine = bufferedReader.readLine()) != null) 
				 { 
					  allLine +=oneLine; 
					   
				  } 
				   bufferedReader.close(); 
 
				  XMLNode xmlnode  = ParseXMLToNode("0",allLine);  
				  System.out.println("#################################################################"); 
 
 
			 	DisplayNode(xmlnode); 
				 //getfirstline node 
			  System.out.println("####################end #############################################"); 
			  //  xmlnode = DelNode(xmlnode,"0.0.2.0" ); 
			   xmlnode = GetNode(xmlnode,"0.0.2.0" ); 
				 System.out.println("GetNode#################################################################"); 
 
 
			 	DisplayNode(xmlnode); 
				 //getfirstline node 
			  System.out.println("####################end #############################################"); 
 
			  String filecontent = Reconstruct(xmlnode); 
			  System.out.println(filecontent); 
			    
		  
			 } catch (IOException e) { 
			out.println(e.toString()); 
		   } 
  
		 out.flush(); 
	//========================================================================= 
 
	} 
 
	public void DisplayNode(XMLNode xmlnode) 
	{ 
		if(xmlnode==null) return; 
	   int nlen = xmlnode.nodenum.length; 
	   for(int i=0;i")[0]; 
			 StringTokenizer  ASection   = new StringTokenizer(arg, " "); 
			int ASectionSize = ASection.countTokens(); 
			if(ASectionSize>0)ASection.nextToken(); 
			for(int j=1;j")[0]; 
				StringTokenizer  ASection   = new StringTokenizer(arg, " "); 
				int ASectionSize = ASection.countTokens();  
				if(ASectionSize>0) xmlnode.barg[i] =  ASection.nextToken()+"  "; 
				for(int j=1;j")[0]; 
				StringTokenizer  ASection   = new StringTokenizer(arg, " "); 
				int ASectionSize = ASection.countTokens();  
				if(ASectionSize>0) xmlnode.barg[i] =  ASection.nextToken()+"  "; 
				for(int j=1;j