dom4j 把 文本 转成 XML

public class CBdom4j {

	private static Document document;

	public static void main(final String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Hello World");
		shell.setBounds(800, 100, 400, 300);
		shell.open();
		final Label lblTest = new Label(shell, SWT.NONE);
		lblTest.setText("测试");
		lblTest.setBounds(10, 10, 40, 20);
		final Text txtTest = new Text(shell, SWT.NONE | SWT.BORDER);
		txtTest.setBounds(50, 10, 80, 20);
		final Button btnTest = new Button(shell, SWT.NONE);
		btnTest.setText("读coi文件");
		btnTest.setBounds(150, 10, 70, 20);
		final Button btnSave = new Button(shell, SWT.NONE);
		btnSave.setText("保存为xml");
		btnSave.setBounds(230, 10, 70, 20);

		btnTest.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {
				System.out.println("测试代码");

				final FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
				fileDialog.setFilterNames(new String[] { "coi Files" });
				fileDialog.setFilterExtensions(new String[] { "*.coi" });
				final String fileSelected = fileDialog.open();
				if (fileSelected != null) {

					document = coiToXML(fileSelected);

				}
			}

		});

		btnSave.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(final SelectionEvent e) {

				final FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
				fileDialog.setFilterNames(new String[] { "xml Files" });
				fileDialog.setFilterExtensions(new String[] { "*.xml" });
				final String fileSelected = fileDialog.open();
				if (fileSelected != null) {
					try {
						final OutputFormat format = OutputFormat.createPrettyPrint();
						final XMLWriter writer = new XMLWriter(new FileWriter(new File(fileSelected)), format);
						document.normalize();
						writer.write(document);
						writer.close();
					} catch (final UnsupportedEncodingException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (final IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
			}

		});

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	public static Document coiToXML(final String fileSelected) {

		Document documentXML = null;
		final File file = new File(fileSelected);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String lineString = null;
			final Document document = DocumentHelper.createDocument();
			Element root = null;
			final List<String> listLine = new ArrayList<String>();
			Boolean isOver = false;
			int numPound = 0;
			while (!isOver && (lineString = reader.readLine()) != null) {
				if (lineString.equals("#")) {
					numPound++;
					if (numPound == 2) {
						isOver = true;
						break;
					}
				}
				listLine.add(lineString);
			}
			reader.close();

			for (int m = 0; m < listLine.size(); m++) {
				lineString = listLine.get(m);
				if (lineString.equals("#")) {// 根节点
					root = document.addElement("root");
				} else if (lineString.startsWith(" ")) {// 根节点下的元素
					final Element v = root.addElement("variable");
					v.addAttribute("name", lineString);
				} else if (lineString.startsWith("#")) {// 根节点下的子节点
					final String[] str = lineString.split("/");
					Element lastElement = root;
					for (int i = 0; i < str.length; i++) {
						final Element v = lastElement.addElement("container");
						v.addAttribute("name", str[i]);
						lastElement = v;
					}
					for (int n = m + 1; n < listLine.size(); n++) {// 此子节点下的元素
						lineString = listLine.get(n);
						if (lineString.startsWith("#")) {// 元素结束,开始下一个子节点
							m = n - 1;
							break;
						}
						final Element v = lastElement.addElement("variable");
						v.addAttribute("name", lineString);
						if (n == listLine.size() - 1) {// 整个文件(根节点)结束
							m = n;
						}
					}

				}
			}

			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter writer = new XMLWriter(System.out, format);
			document.normalize();
			documentXML = document;
			writer.write(document);
			writer.close();

		} catch (final IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (final IOException e1) {
				}
			}
		}
		return documentXML;
	}
}

#
 *RESERVED GF7
 *RESERVED GF8
 *RESERVED GF9
 *RESERVED_DRIVER_STATUS GI0
 *RESERVED_DRIVER_STATUS GI1
#CAR_2/all_doors_close/contact_no_R_DO_1_closed_1/
 entree1 IF325
 entree2 IF326
#CAR_2/all_doors_close/contact_no_R_DO_2_closed_1/
 entree1 IF327
 entree2 IF328
 etat IB479
#CAR_2/all_doors_close/contact_no_R_DO_3_closed_1/
 entree1 IF329
 entree2 IF330
#CAR_2/all_doors_close/masse1/
 entree1 IF331
 sortie1 OF318
#CAR_2/all_doors_close/noeud1/
 entree1 IF316
 entree2 IF259
#CAR_2/all_doors_open/
 R_DO_1_opened IB484
 R_DO_2_opened IB485
#CAR_6/all_doors_open/contact_no_R_DO_3_opened_1/
 CHANGEMENT GB1968
#CAR_6/all_doors_open/noeud1/
 CHANGEMENT GB1968
#
 clientexecstatus LI681
 pid LI682
 mxsimucounter LI683
 mxmicrostepcounter LI684
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值