📄 702164.xml
字号:
		//}
	}
	
	public void displayDocument(String xml,int num)
	{
		try
		{
			DOMParser parser = new DOMParser();
			parser.parse(xml);
			doc = parser.getDocument();
			display(doc,"",num);
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
	}
	//add the data to the upsinfor.xml
	public  void createDoc(String s,String[] data,int line)
	{
		//displayDocument(s,1);
		this.displayStrings = data;
		this.numberDisplayLines = line;
		try
		{
			FileWriter filewriter = new FileWriter(s);
			for(int loopIndex=0;loopIndex <numberDisplayLines;loopIndex++)
			{
				filewriter.write(displayStrings[loopIndex].toCharArray());
				filewriter.write('\n');
			}
			filewriter.close();
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
		//debug code
		for(int loopIndex = 0;loopIndex < numberDisplayLines;loopIndex++)
		{
			System.out.println(displayStrings[loopIndex]);
		}
	}
	/*
	pubic void setData(String s)
	{
		displayStrings[numberDisplayLines++] = s;
	}
	*/
	//edit the data of the upsinfor.xml
	public  void editDoc(String s,String index)
	{
		this.index = index;
		displayDocument(s,2);
		try
		{
			FileWriter filewriter = new FileWriter(s);
			for(int loopIndex=0;loopIndex <numberDisplayLines;loopIndex++)
			{
				filewriter.write(displayStrings[loopIndex].toCharArray());
				filewriter.write('\n');
			}
			filewriter.close();
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
		//debug code
		for(int loopIndex = 0;loopIndex < numberDisplayLines;loopIndex++)
		{
			System.out.println(displayStrings[loopIndex]);
		}
	}
	//add  data method
	public void addUps(Node node)
	{
				if(node.getNodeName().equals("DOCUMENT"))
				{
					String[] childName = {"NAME","IP","ZONE","POSITION","CODE","TIB"};
					Element[] childNode = new Element[childName.length];
					Text[] textNode = new Text[childName.length];
					Element addNode = doc.createElement("UPS");
					for(int i =0;i<childName.length;i++)
					{
						childNode[i] = doc.createElement(childName[i]);
						textNode[i] = doc.createTextNode(str[i]);
					}
				
					for(int j =0;j<childName.length;j++)
					{
						childNode[j].appendChild(textNode[j]);
						addNode.appendChild(childNode[j]);
					}
					node.appendChild(addNode);
				}		
	}
	//edit data method
	public void editUps(Node node)
	{
		int count = 0;
		if(node.getNodeName().equals("NAME"))
		{
			NodeList list = node.getChildNodes();
			for(int i=0;i<list.getLength();i++)
			{
				if(list.item(i).getNodeValue().trim().equals(index))
				{
					//System.out.println(list.item(i).getParentNode().getParentNode().getNodeName().trim());
					NodeList nodeList = list.item(i).getParentNode().getParentNode().getChildNodes();
					for(int j=0;j<nodeList.getLength();j++)
					{
					//System.out.println(nodeList.item(j).getNodeName()+"  ");
						NodeList nd = nodeList.item(j).getChildNodes();
						for(int k=0;k<nd.getLength();k++)
						{
						//	System.out.println(nd.item(k).getNodeValue());
							nd.item(k).setNodeValue(str[count]);
							count++;
						}
					}
					//System.out.println(node.getNodeName()+"'s value is "+list.item(i).getNodeValue());
				}
			}
		}		
	}
	//display the xml document's data
	public void display(Node node,String indent,int flag)
	{
		if(node==null)
		{return;}
		int type = node.getNodeType();
		switch(type)
		{
			//deal with the document node
			case Node.DOCUMENT_NODE:
			{
				displayStrings[numberDisplayLines]=indent;
				
				displayStrings[numberDisplayLines]+=
				"<?xml version=\"1.0\" encoding=\""+
				"GB2312"+"\"?>";
				
				numberDisplayLines++;
				display(((Document)node).getDocumentElement(),"",flag);
				break;
			}
			//deal with the elements node
			case Node.ELEMENT_NODE:
			{
				//add ups data
				if(flag==1)
				{addUps(node);}
				//edit the ups data 				
				if(flag==2)
				{editUps(node);}
				//display the data 
				displayStrings[numberDisplayLines] = indent;
				displayStrings[numberDisplayLines]+="<";
				displayStrings[numberDisplayLines]+=node.getNodeName();
				//deal with the elements node's attribute
				int length =(node.getAttributes()!=null)?
				node.getAttributes().getLength():0;
				Attr attributes[] = new Attr[length];
				for(int loopIndex=0;loopIndex<length;loopIndex++)
				{
					attributes[loopIndex] = 
					(Attr)node.getAttributes().item(loopIndex);
				}
				for(int loopIndex=0;loopIndex<attributes.length;loopIndex++)
				{
					Attr attribute = attributes[loopIndex];
					displayStrings[numberDisplayLines]+=" ";
					displayStrings[numberDisplayLines]+= attribute.getNodeName();
					displayStrings[numberDisplayLines]+="=\"";
					displayStrings[numberDisplayLines]+= attribute.getNodeValue();
					displayStrings[numberDisplayLines]+="\"";
				}
				displayStrings[numberDisplayLines]+=">";
				
				//deal with the node's child node
				numberDisplayLines++;
				NodeList childNodes = node.getChildNodes();
				if(childNodes!=null)
				{
					length = childNodes.getLength();
					indent += " ";
					for(int loopIndex=0;loopIndex<length;loopIndex++)
					{
						display(childNodes.item(loopIndex),indent,flag);
					}					
				}		
				break;			
			}
			/*
			case Node.CDATA_SECTION_NODE:
			{
				displayStrings[numberDisplayLines] = indent;
				displayStrings[numberDisplayLines] +="<![CDATA[";
				displayStrings[numberDisplayLines] +=node.getNodeValue();
				displayStrings[numberDisplayLines] +="]]>";
				numberDisplayLines++;
				break;
			}
			*/
			case Node.TEXT_NODE:
			{
				displayStrings[numberDisplayLines] = indent;
				String newText = node.getNodeValue().trim();
				if(newText.indexOf("\n") < 0 && newText.length() > 0)
				{
					displayStrings[numberDisplayLines] += newText;
					numberDisplayLines++;
				}
				break;
			}
					
			case Node.PROCESSING_INSTRUCTION_NODE:
			{
				displayStrings[numberDisplayLines] = indent;
				displayStrings[numberDisplayLines] +="<?";
				displayStrings[numberDisplayLines] += node.getNodeName();
				String text = node.getNodeValue();
				if(text != null && text.length()> 0)
				{
					displayStrings[numberDisplayLines] += text;
				}
				displayStrings[numberDisplayLines] +="?>";
				numberDisplayLines++;
				break;
			}
				
		}
		if(type == Node.ELEMENT_NODE)
		{
			displayStrings[numberDisplayLines] = indent.substring(0,indent.length()-1);
			displayStrings[numberDisplayLines] +="</";
			displayStrings[numberDisplayLines] += node.getNodeName();
			displayStrings[numberDisplayLines] += ">";
			numberDisplayLines++;
			indent += "    ";
		}
	}	
}
</Content>
<PostDateTime>2002-5-7 22:54:41</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>无影</PostUserNickName>
<rank>二级(初级)</rank>
<ranknum>user2</ranknum>
<credit>100</credit>
<ReplyID>4611882</ReplyID>
<TopicID>702164</TopicID>
<PostUserId>71921</PostUserId>
<PostUserName>obliang</PostUserName>
<Point>0</Point>
<Content>希望对JSP与XML感兴趣的朋友有帮助。
</Content>
<PostDateTime>2002-5-7 22:55:56</PostDateTime>
</Reply>
<Reply>
<PostUserNickName></PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>98</credit>
<ReplyID>4614524</ReplyID>
<TopicID>702164</TopicID>
<PostUserId>247628</PostUserId>
<PostUserName>jevon00</PostUserName>
<Point>0</Point>
<Content>搞定,谢各位大哥了。</Content>
<PostDateTime>2002-5-8 10:05:46</PostDateTime>
</Reply>
</Replys>
</Topic>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -