⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsexml.java

📁 增加修改删除xml节点 使用resin 放到webapp下,目录xml 访问http://localhost:8080/xml/parsexml 在httpd.exe 中观察打印的变化
💻 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<nlen;i++) 
			{
				  arg[i] = arg[i].trim()+">";
			}
			 int i=0;
			  while( i<nlen )
			 {	
				   
				  if(arg[i]==null) 
				 {
					  i++;
					  continue;
				 }
					  
				 
				  if(arg[i].startsWith("<?") || arg[i].startsWith("<!")  )
				  {
					  barg[thiscount] = arg[i];
					  earg[thiscount] ="";
					   argnum[thiscount] = nodenum+"."+String.valueOf(thiscount);
						  xmlnodechild[thiscount] =null;
					  thiscount++; 
					//  System.out.println(arg[i]+":arg[i].startsWith(\"<?\") || arg[i].startsWith(\"<!\") ");
					
				  }
				  else
				  {
					  
					  String sfind[] = arg[i].split(">");
					  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<nlen;j++)
						  {
							 if(arg[j]==null) continue;

							 arg[j] = arg[j].trim() ;
							 if(arg[j].startsWith("<"+sfind[1]+" ") 
								 ||arg[j].startsWith("<"+sfind[1]+">") 
								 ) 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;k<j;k++)
									  {
										 if(arg[k]!=null) newsrc += arg[k] ;
									  } 

								 
									 
									  if(newsrc!=null && ! newsrc.trim().equals(""))
									  {
										  
											xmlnodechild[ thiscount] = ParseXMLToNode(nodenum+"."+String.valueOf(thiscount ),  newsrc);
								 
										   
									  }

									 
								  } 
								  else
								  {
										  xmlnodechild[thiscount] =null;
								  }

								   thiscount++;	

								  i=j;			 
								  break;
							  }//  if(nshouldfind==nhavefind) 
						  }//for(int j=i+1;j<nlen;j++) 
	 
					 
					  }//if(sfind.length>1)

				  }

			   i++;
	 
			 }//for(int i=0;i<nlen ;i++)

			if(thiscount==0) return null;
			 xmlnode.nodenum = new String[thiscount];
			  xmlnode.barg = new String[thiscount];
			   xmlnode.earg = new String[thiscount]; 
			   xmlnode.xmlnode = new XMLNode [thiscount];
	 
			 for( i=0;i<thiscount;i++)
			{ 
				 xmlnode.nodenum[i]=  argnum[i]; 
				 xmlnode.barg[i] =     barg[i];
				 xmlnode.earg[i] =     earg[i]; 
				 xmlnode.xmlnode[i] = xmlnodechild[i];
				System.out.println(argnum[i]); 
			 }

		 

		 

			return xmlnode;
 
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
	                             throws ServletException, IOException {

		res.setHeader("Cache-Control","no-store"); //HTTP 1.1 
		res.setHeader("Pragma","no-cache"); //HTTP 1.0 
		res.setDateHeader ("Expires", 0); //prevents caching at the proxy server 
		res.setContentType("text/html;charset=gb2312"); 
		PrintWriter out = res.getWriter();		

		
	 out.println("<html><body> ");

			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<nlen;i++)
	  { 
		   System.out.println("     "+xmlnode.nodenum[i]);
			System.out.println("     "+xmlnode.barg[i]);
			System.out.println("     "+xmlnode.earg[i]); 
			//parse all arg
			String arg = xmlnode.barg[i].split(">")[0];
			 StringTokenizer  ASection   = new StringTokenizer(arg, " ");
			int ASectionSize = ASection.countTokens();
			if(ASectionSize>0)ASection.nextToken();
			for(int j=1;j<ASectionSize;j++)
		   {
				 System.out.println("               "+ASection.nextToken());
		   }
		   if(xmlnode.xmlnode[i]!=null) DisplayNode(xmlnode.xmlnode[i]);
		  
	
	   }

	    
	}

	public String Reconstruct(XMLNode xmlnode)
	{
		String filecontent = "";
		if(xmlnode==null) return "";
	   int nlen = xmlnode.nodenum.length;
	   for(int i=0;i<nlen;i++)
	  { 
		   filecontent += xmlnode.barg[i]+"\r\n";
		   if(xmlnode.xmlnode!=null)  filecontent += Reconstruct(xmlnode.xmlnode[i]);
		   filecontent += xmlnode.earg[i]+"\r\n";
	
	   }
	   return filecontent;
	}

	public XMLNode AddNodeValue(XMLNode xmlnode ,String nodenum,String addvalue,String myvalue)
	{
	   int nlen = xmlnode.nodenum.length;
	   for(int i=0;i<nlen;i++)
	  { 
		    System.out.println("     "+xmlnode.nodenum[i]);
			// System.out.println("     nodenum="+nodenum);
			//System.out.println("     "+xmlnode.earg[i]); 
			if (xmlnode.nodenum[i]!=null && xmlnode.nodenum[i].equals(nodenum))
			{
				String arg = xmlnode.barg[i].split(">")[0];
				StringTokenizer  ASection   = new StringTokenizer(arg, " ");
				int ASectionSize = ASection.countTokens(); 
				if(ASectionSize>0) xmlnode.barg[i] =  ASection.nextToken()+"  ";
				for(int j=1;j<ASectionSize;j++)
				{
				    String param = ASection.nextToken() ;
				 
					 xmlnode.barg[i] += param +" ";
				}

				xmlnode.barg[i] += addvalue+"="+myvalue+" ";

				 xmlnode.barg[i] += ">"; 
				 return xmlnode;
				
		   
			}
			else if(xmlnode.xmlnode[i]!=null)
		  {
				xmlnode.xmlnode[i] = ReplaceNodeValue(xmlnode.xmlnode[i],nodenum,addvalue,myvalue);
		  }
	
	   }

	   return xmlnode;
	}

	public XMLNode AddNode(XMLNode xmlnode ,String nodenum,String barg,String earg)
	{
	   int nlen = xmlnode.nodenum.length;
	   for(int i=0;i<nlen;i++)
	  { 
		    System.out.println("     "+xmlnode.nodenum[i]);
			// System.out.println("     nodenum="+nodenum);
			//System.out.println("     "+xmlnode.earg[i]); 
			if (xmlnode.nodenum[i]!=null && xmlnode.nodenum[i].equals(nodenum))
			{ 
				XMLNode xmlnodeadd = new XMLNode();
				xmlnodeadd.nodenum = new String[nlen+1];
				xmlnodeadd.barg = new String[nlen+1];
				xmlnodeadd.earg = new String[nlen+1]; 
				xmlnodeadd.xmlnode = new XMLNode [nlen+1];
				int nadd = 0;
				for(int j=0;j<nlen;j++)
				{
					 xmlnodeadd.nodenum[nadd]=  xmlnode.nodenum[j]; 
					 xmlnodeadd.barg[nadd] =    xmlnode. barg[j];
					 xmlnodeadd.earg[nadd] =     xmlnode.earg[j]; 
					 xmlnodeadd.xmlnode[nadd] = xmlnode.xmlnode[j];
					 nadd++;
					if(i==j) 
					{
						 xmlnodeadd.nodenum[nadd]=   xmlnode.nodenum[j]+".a"; 
						 xmlnodeadd.barg[nadd] =     barg;
						 xmlnodeadd.earg[nadd] =     earg; 
						 xmlnodeadd.xmlnode[nadd] = null;
						 nadd++;
					}
					
				}
				return xmlnodeadd; 		   
			}
			else if(xmlnode.xmlnode[i]!=null)
		  {
				xmlnode.xmlnode[i] = AddNode(  xmlnode.xmlnode[i] ,  nodenum,  barg,  earg);
		  }
	
	   }

	   return xmlnode;
	}


	public XMLNode GetNode(XMLNode xmlnode ,String nodenum )
	{
	   int nlen = xmlnode.nodenum.length;
	   XMLNode getnode =  null;
	   for(int i=0;i<nlen;i++)
	  { 
		    System.out.println("    GetNode "+xmlnode.nodenum[i]);
			 System.out.println("    GetNode nodenum="+nodenum);
			//System.out.println("     "+xmlnode.earg[i]); 
			if (xmlnode.nodenum[i]!=null && xmlnode.nodenum[i].equals(nodenum))
			{  
				System.out.println("  ok ");
				return xmlnode; 		   
			}
			else if(xmlnode.xmlnode[i]!=null)
		  {
				getnode = GetNode(  xmlnode.xmlnode[i] ,  nodenum );
				if(getnode!=null) return getnode;
		  }
	
	   }

	   return getnode;
	}

	public XMLNode DelNode(XMLNode xmlnode ,String nodenum )
	{
	   int nlen = xmlnode.nodenum.length;
	   for(int i=0;i<nlen;i++)
	  { 
		    System.out.println("     "+xmlnode.nodenum[i]);
			 System.out.println("     nodenum="+nodenum);
			//System.out.println("     "+xmlnode.earg[i]); 
			if (xmlnode.nodenum[i]!=null && xmlnode.nodenum[i].equals(nodenum))
			{ 
				XMLNode xmlnodedel = new XMLNode();
				xmlnodedel.nodenum = new String[nlen-1];
				xmlnodedel.barg = new String[nlen-1];
				xmlnodedel.earg = new String[nlen-1]; 
				xmlnodedel.xmlnode = new XMLNode [nlen-1];
				int nadd = 0;
				for(int j=0;j<nlen;j++)
				{ 
					if(i!=j) 
					{
						 xmlnodedel.nodenum[nadd]=  xmlnode.nodenum[j]; 
						 xmlnodedel.barg[nadd] =    xmlnode. barg[j];
						 xmlnodedel.earg[nadd] =     xmlnode.earg[j]; 
						 xmlnodedel.xmlnode[nadd] = xmlnode.xmlnode[j];
						 nadd++;
					} 
				}
				return xmlnodedel; 		   
			}
			else if(xmlnode.xmlnode[i]!=null)
		  {
				xmlnode.xmlnode[i] = DelNode(  xmlnode.xmlnode[i] ,  nodenum );
		  }
	
	   }

	   return xmlnode;
	}

	public XMLNode ReplaceNodeValue(XMLNode xmlnode ,String nodenum,String findvalue,String revalue) 
	{
	 
	   int nlen = xmlnode.nodenum.length;
	   for(int i=0;i<nlen;i++)
	  { 
		    System.out.println("     "+xmlnode.nodenum[i]);
			 System.out.println("     nodenum="+nodenum);
			//System.out.println("     "+xmlnode.earg[i]); 
			if (xmlnode.nodenum[i]!=null && xmlnode.nodenum[i].equals(nodenum))
			{
				String arg = xmlnode.barg[i].split(">")[0];
				StringTokenizer  ASection   = new StringTokenizer(arg, " ");
				int ASectionSize = ASection.countTokens(); 
				if(ASectionSize>0) xmlnode.barg[i] =  ASection.nextToken()+"  ";
				for(int j=1;j<ASectionSize;j++)
				{
				    String param = ASection.nextToken() ;
					if(param.trim().startsWith(findvalue))
					{
						param = findvalue+"="+revalue;
					}
					 xmlnode.barg[i] += param +" ";
				}

				 xmlnode.barg[i] += ">"; 
				 return xmlnode;
				
		   
			}
			else if(xmlnode.xmlnode[i]!=null)
		  {
				xmlnode.xmlnode[i] = ReplaceNodeValue(xmlnode.xmlnode[i],nodenum,findvalue,revalue);
		  }
	
	   }

	   return xmlnode;
	}
	public String getServletInfo() {
		return "A servlet that knows the name of the person to whom it's" +
		       "saying hello";
	}

	public void doPost(HttpServletRequest request,
	HttpServletResponse response)
	throws ServletException, IOException {
	 
		doGet(  request,  response);
	}
}//end here! 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -