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

📄 xmlparser.java

📁 xml解析工具,可以完成对xml的创建,查询等.
💻 JAVA
字号:
/*
 * 创建日期 2004-10-17
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package mg.sdu.util;

/**
 * @author tonny
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
import java.io.*; 
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xml.serialize.*;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.net.*;

public class XmlParser {

	private String FileName="ServiceUI.xml";
	
	public XmlParser(){

	}
	//获得DocumentBuilder
	public   DocumentBuilder getDocumentBuilder()
   {
	   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	   DocumentBuilder builder = null;
	   factory.setValidating(false);
	   factory.setNamespaceAware(true);
	   try
	   {
		   builder = factory.newDocumentBuilder();
	   }
	   catch(Exception e)
	   {
		   System.out.println("XMLUtil.getDocumentBuilder Exception:");
		   e.printStackTrace();
	   }
	   return builder;
   }

   public   Document getNewDocument()
   {
	   DocumentBuilder builder = getDocumentBuilder();
	   return builder.newDocument();
   }

   public   Document getDocumentFromString(String xmlStr)
   {
	   Document doc = null;
	   doc = getDocument(new StringReader(xmlStr));
	   return doc;
   }

   public   Document getDocumentFromFile(File file)
   {
	   return getDocumentFromFile(file.getAbsolutePath());
   }

   public   Document getDocumentFromURI(String uri)
	   throws Exception
   {
	   PrintWriter providerWriter = new PrintWriter(System.out);
	   URL myWSDLLink = new URL(uri);
	   URLConnection myURIConnection = myWSDLLink.openConnection();
	   InputStreamReader myInputReader = new InputStreamReader(myURIConnection.getInputStream());
	   return getDocument(myInputReader);
   }

   public   Document getDocumentFromFile(String fileName)
   {
	   Document doc = null;
	   try
	   {
		   doc = getDocument(new InputStreamReader(new FileInputStream(fileName)));
	   }
	   catch(IOException ioe)
	   {
		   System.out.println("Exception while trying to create document from file:" + fileName);
	   }
	   return doc;
   }

   public   Node getRootNodeFromString(String xmlStr)
   {
	   Document doc = getDocumentFromString(xmlStr);
	   Node rootNode = doc.getFirstChild();
	   return rootNode;
   }

   public   NodeList queryByXPath(Node node, String queryString)
   {
	   try
	   {
		   return XPathAPI.selectNodeList(node, queryString);
	   }
	   catch(Exception e)
	   {
		   e.printStackTrace();
	   }
	   return null;
   }
   
   public Node querySingleNodeByXPath(Node node,String queryString)
   {
   	  try
   	  {
   	  	  return XPathAPI.selectSingleNode(node,queryString);
   	  }
   	  catch(Exception e)
   	  {
   	  	 e.printStackTrace();
   	  }
	return null;
   	  
   }

   public   Document getDocument(Reader reader)
   {
	   Document document = null;
	   DocumentBuilder builder = getDocumentBuilder();
	   if(builder != null)
		   try
		   {

			   document = builder.parse(new InputSource(reader));
		   }
		   catch(SAXException se)
		   {
			   System.out.println("Exception parsing XML document.");
			   se.printStackTrace();
		   }
		   catch(IOException ie)
		   {
			   System.out.println("Exception reading XML document.");
		   }
	   return document;
   }


   public   Document getDocument(InputStream steam)
   {
	   Document document = null;
	   DocumentBuilder builder = getDocumentBuilder();
	   if(builder != null)
		   try
		   {

			   document = builder.parse(steam);
		   }
		   catch(SAXException se)
		   {
			   System.out.println("Exception parsing XML document.");
			   se.printStackTrace();
		   }
		   catch(IOException ie)
		   {
			   System.out.println("Exception reading XML document.");
		   }
	   return document;
   }

   public   String DocumentToString(Document doc)
	   throws Exception
   {
	   StringWriter sw = new StringWriter();
	   writeDocument(sw, doc);
	   return sw.toString();
   }

   public   void writeDocumentToFile(String fileName, Document doc)
   {
	   try
	   {
		   writeDocument(new FileWriter(fileName), doc);
	   }
	   catch(IOException ioe)
	   {
		   System.out.println("Exception while trying to write document to file: " + fileName);
	   }
   }

   public   void writeDocumentToFile(File file, Document doc)
   {
	   try
	   {
		   writeDocument(new FileWriter(file), doc);
	   }
	   catch(IOException ioe)
	   {
		   System.out.println("Exception while trying to write document to file: " + file.getName());
	   }
   }

   public   void writeDocument(Writer writer, Document document)
   {
	   if(writer != null)
		   try
		   {
			   getSerializer(writer).serialize(document);
		   }
		   catch(IOException e)
		   {
			   System.out.println("Could not serialize.");
		   }
   }

   protected   XMLSerializer getSerializer(Writer writer)
   {
	   OutputFormat outputFormat = new OutputFormat();
	   outputFormat.setMethod("xml");
	   outputFormat.setPreserveSpace(true);
	   outputFormat.setIndenting(true);
	   outputFormat.setIndent(4);
	   return new XMLSerializer(writer, outputFormat);
   }

   public   void replaceChild(Node old_node, Node new_node)
   {
	   if(old_node != new_node)
		   old_node.getParentNode().replaceChild(new_node, old_node);
   }

   public   void appendChild(Node parent, Node child)
   {
	   parent.appendChild(child);
   }

   public   Node createNode(Document doc, String label, HashMap items)
   {
	   Element e = doc.createElement(label);
	   if(items != null && items.size() > 0)
	   {
		   String key;
		   String value;
		   for(Iterator it = items.keySet().iterator(); it.hasNext(); e.setAttribute(key, value))
		   {
			   key = (String)it.next();
			   value = (String)items.get(key);
		   }

	   }
	   return (Node)e;
   }
	public void AddServiceUI(){
			
	}
	
	
	public void modifyElement(Element element) {

		 // loop through child nodes
		 Node child;
		 Node next = (Node)element.getFirstChild();
		 while ((child = next) != null) {
		   // set next before we change anything
		   next = child.getNextSibling();

		   // handle child by node type
		   if (child.getNodeType() == Node.TEXT_NODE) {
			// trim whitespace from content text
			String trimmed = child.getNodeValue().trim();
			if (trimmed.length() == 0) {

			  // delete child if nothing but whitespace
			  element.removeChild(child);

			} else {

			  // create a "text" element matching parent namespace
			  Document doc = element.getOwnerDocument();
			  String prefix = element.getPrefix();
			  String name = (prefix == null) ? "text" : (prefix + ":text");
			  Element text = 
				doc.createElementNS(element.getNamespaceURI(), name);
		  // wrap the trimmed content with new element
			  text.appendChild(doc.createTextNode(trimmed));
			  element.replaceChild(text, child);

			}
		  } else if (child.getNodeType() == Node.ELEMENT_NODE) {

			// handle child elements with recursive call
			modifyElement((Element)child);

		  }
		}
	  }

}

⌨️ 快捷键说明

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