﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-零一的伟大</title><link>http://www.blogjava.net/arrowfly/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 28 Apr 2026 21:39:07 GMT</lastBuildDate><pubDate>Tue, 28 Apr 2026 21:39:07 GMT</pubDate><ttl>60</ttl><item><title>读取XML来创建菜单</title><link>http://www.blogjava.net/arrowfly/archive/2006/11/27/83782.html</link><dc:creator>零一的伟大</dc:creator><author>零一的伟大</author><pubDate>Mon, 27 Nov 2006 05:48:00 GMT</pubDate><guid>http://www.blogjava.net/arrowfly/archive/2006/11/27/83782.html</guid><wfw:comment>http://www.blogjava.net/arrowfly/comments/83782.html</wfw:comment><comments>http://www.blogjava.net/arrowfly/archive/2006/11/27/83782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/arrowfly/comments/commentRss/83782.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/arrowfly/services/trackbacks/83782.html</trackback:ping><description><![CDATA[
		<table cellspacing="1" cellpadding="2" width="100%" border="0">
				<tbody>
						<tr>
								<td>
										<strong>Type:</strong>
										<br />完整代码</td>
								<td>
										<strong>Category:</strong>
										<br />其它 </td>
						</tr>
						<tr>
								<td>
										<strong>License:</strong>
										<br />GNU General Public License</td>
								<td>
										<strong>Language:</strong>
										<br />Java </td>
						</tr>
						<tr>
								<td colspan="2"> <br /><strong>Description:</strong><br />通过读取XML文档来创建菜单<br />XML的DTD规范是：<br />&lt;!ELEMENT root (top+)&gt;<br />&lt;!ELEMENT top ((menu*,separator*,popup*)*)&gt;<br />&lt;!ATTLIST top name ID #REQUIRED&gt;<br />&lt;!ELEMENT menu (#PCDATA)&gt;<br />&lt;!ELEMENT separator EMPTY&gt;<br />&lt;!ELEMENT popup ((menu+,separator*)*)&gt;<br />&lt;!ATTLIST popup name ID #REQUIRED&gt; </td>
						</tr>
				</tbody>
		</table>
		<h2> </h2>
		<h2>最新代码版本： :1.0</h2>
		<p>
		</p>
		<pre>
				<span style="FONT-SIZE: smaller">
						<font size="1">/**
 * 标题：制作菜单
 * 
 * 注：通过XML就可以制作出菜单来
 * 
 * 作者：元杰(夏祥均)
 * 时间：2006.09.28
 */
package yuanjie.myconfig.frame;

import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class CreateMenu 
{
	private String fileName = null;
	private ActionListener actionListener = null;
	private JMenuBar rootMenu = null;
	//构造函数
	public CreateMenu(String fileName, ActionListener action)
	{
		this.fileName = fileName;
		this.actionListener = action;
		this.rootMenu = new JMenuBar();
		init();
	}
	//返回菜单的内容
	public JMenuBar getMenuBar() 
	{
		return rootMenu;
	}
	//初始化菜单
	private void init()
	{
		//读取XML文档
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try{
			db = dbf.newDocumentBuilder();
		} catch(ParserConfigurationException pce)
		{
			System.err.println(pce.getMessage());
			System.exit(1);
		}
		//加载XML文档
		Document doc = null;
		try{
			doc = db.parse(this.fileName);
		} catch(DOMException dom)
		{
			System.err.println(dom.getMessage());
			System.exit(1);
		} catch(Exception ioe)
		{
			System.err.println(ioe.getMessage());
			System.exit(1);
		}
		//开始读取数据
		Element root = doc.getDocumentElement();
		//开始top的读取
		NodeList top = root.getElementsByTagName("top");
		
		for (int i = 0; i &lt; top.getLength(); i++)
		{
			Element menu = (Element) top.item(i); 
			//创建顶层菜单
			String menuName = menu.getAttribute("name");
			JMenu topMenu = new JMenu(menuName);
			this.rootMenu.add(topMenu);
			
			NodeList menuChild = menu.getChildNodes();
			for(int j = 1; j &lt; menuChild.getLength(); j += 2)
			{
				//创建次级菜单
				Element child = (Element) menuChild.item(j);
				String nodeName = child.getNodeName();
				if(nodeName.equals("separator"))
				{
					//为分隔线
					topMenu.addSeparator();
				} else if(nodeName.equals("menu"))
				{
					//菜单项
					NodeList n1 = child.getChildNodes();
					//菜单标题
					String str1 = n1.item(0).getNodeValue();
					//添加菜单
					JMenuItem mItem = new JMenuItem(str1);
					topMenu.add(mItem);
					mItem.addActionListener(this.actionListener);
				} else if(nodeName.equals("popup"))
				{
					//弹出菜单项
					String str2 = child.getAttribute("name");
					JMenu popupMenu = new JMenu(str2);
					//添加弹出菜单
					topMenu.add(popupMenu);
					NodeList popup = child.getChildNodes();
					for(int k = 1; k &lt; popup.getLength(); k += 2)
					{
						Element popupChild = (Element) popup.item(k);
						String popupName = popupChild.getNodeName();
						if(popupName.equals("separator"))
						{
							//为分隔线
							popupMenu.addSeparator();
						} else if(popupName.equals("menu"))
						{
							//菜单项
							NodeList n3 = popupChild.getChildNodes();
							String str3 = n3.item(0).getNodeValue();
							JMenuItem pItem = new JMenuItem(str3);
							popupMenu.add(pItem);
							pItem.addActionListener(this.actionListener);
						}
					}
				}
			}
		}
	}
}

<br /><br /><br />来源：<a href="http://gforge.osdn.net.cn/snippet/detail.php?type=snippet&amp;id=9">http://gforge.osdn.net.cn/snippet/detail.php?type=snippet&amp;id=9</a></font>
				</span>
		</pre>
<img src ="http://www.blogjava.net/arrowfly/aggbug/83782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/arrowfly/" target="_blank">零一的伟大</a> 2006-11-27 13:48 <a href="http://www.blogjava.net/arrowfly/archive/2006/11/27/83782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>