提取xml文档的内容到.txt文件中

前端之家收集整理的这篇文章主要介绍了提取xml文档的内容到.txt文件中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是xml文档

<?xml version="1.0" encoding="ISO-8859-1"?>
<JDBOR date="2014-07-01 04:13:13" version="1.0.20 [2012-12-14]" copyright="Orphanet (c) 2014">
<DisorderList count="6942">
<Disorder id="17601">/
<OrphaNumber>166024</OrphaNumber>/
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.PHP?lng=en&amp;Expert=166024</ExpertLink>
<Name lang="en">Multiple epiphyseal dysplasia,Al-Gazali type</Name>/
<DisorderFlagList count="1">
<DisorderFlag id="2"/>
</DisorderFlagList>
<SynonymList count="1">
<Synonym lang="en">Multiple epiphyseal dysplasia - macrocephaly - distinctive facies</Synonym>
</SynonymList>
<ExternalReferenceList count="2">
<ExternalReference id="38949">
<Source>OMIM</Source>/
<Reference>607131</Reference>/
</ExternalReference>
<ExternalReference id="78407">/
<Source>ICD10</Source>/
<Reference>Q77.3</Reference>/
</ExternalReference>
</ExternalReferenceList>
</Disorder>
<Disorder id="17603">
<OrphaNumber>166032</OrphaNumber>
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.PHP?lng=en&amp;Expert=166032</ExpertLink>
<Name lang="en">Multiple epiphyseal dysplasia,with miniepiphyses</Name>
<DisorderFlagList count="1">
<DisorderFlag id="2"/>
</DisorderFlagList>
<SynonymList count="0">
</SynonymList>
<ExternalReferenceList count="2">
<ExternalReference id="38953">
<Source>OMIM</Source>
<Reference>609325</Reference>
</ExternalReference>
<ExternalReference id="78405">
<Source>ICD10</Source>
<Reference>Q77.3</Reference>
</ExternalReference>
</ExternalReferenceList>
</Disorder>

</DisorderList>
</JDBOR>

提取出来的.txt文件的数据

这是en_product1_Result_OMIM.txt文档

17601 166024Multiple epiphyseal dysplasia,Al-Gazali type607131/
17603 166032 Multiple epiphyseal dysplasia,with miniepiphyses 609325/
2 58 Alexander disease203450/
17602 166029 Multiple epiphyseal dysplasia,with severe proximal femoral dysplasia609324/

对应的Java代码

<pre name="code" class="java">package com.yanek.demo.xml.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.util.ArrayList;

public class xmltran1 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		// dom4,xml

		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(new File("D:\\root\\data\\en_product1.xml"));
			Element root = document.getRootElement();//得到根节点
			FileWriter f=new FileWriter(new File("d:\\en_product1_Result_OMIM.txt"),true);
			FileWriter f1=new FileWriter(new File("d:\\en_product1_Result_MSEH.txt"),true);
			//FileWriter f1=new FileWriter(new File("E:\\新建文件夹\\a.txt"),true);
			FileWriter f2=new FileWriter(new File("d:\\en_product1_Result_UMLS.txt"),true);
			FileWriter f3=new FileWriter(new File("d:\\en_product1_Result_ICD.txt"),true);
			BufferedWriter writer1 = new BufferedWriter(f);
			BufferedWriter writer2 = new BufferedWriter(f1);
			BufferedWriter writer3 = new BufferedWriter(f2);
			BufferedWriter writer4 = new BufferedWriter(f3);
			List actions = root.elements("DisorderList");
			for(int i = 0; i < actions.size(); i++){
				Element action = (Element) actions.get(i);
				List list1 = action.elements("Disorder");
				for(int j = 0; j < list1.size(); j++){
					Element element1 = (Element) list1.get(j);
					System.out.println("Disorder id:"+ element1.attributeValue("id"));
					writer1.write(element1.attributeValue("id")+"\t");
					writer2.write(element1.attributeValue("id")+"\t");
					writer3.write(element1.attributeValue("id")+"\t");
					writer4.write(element1.attributeValue("id")+"\t");
					List list2 = element1.elements("OrphaNumber");
					List<String> ls2 = new ArrayList<String>();
					Element element6 = (Element) list2.get(0);
					ls2.add(element6.getText());
					System.out.println("OrphaNumber:"+ element6.getText());
					writer1.write(ls2.get(0)+"\t");
					writer2.write(ls2.get(0)+"\t");
					writer3.write(ls2.get(0)+"\t");
					writer4.write(ls2.get(0)+"\t");
					List list3 = element1.elements("Name");
					List<String> ls3 = new ArrayList<String>();
					Element element5 = (Element) list3.get(0);
					ls3.add(element5.getText());
					System.out.println("Name:"+ element5.getText());
					writer1.write(ls3.get(0)+"\t");
					writer2.write(ls3.get(0)+"\t");
					writer3.write(ls3.get(0)+"\t");
					writer4.write(ls3.get(0)+"\t");
					List list4 = element1.elements("ExternalReferenceList");
					for (int m = 0; m < list4.size(); m++) {
						Element element2 = (Element) list4.get(m);
						List list5 = element2.elements("ExternalReference");
						for(int p = 0; p < list5.size(); p++){
							Element element3 = (Element) list5.get(p);
							System.out.println("ExternalReference id:"+ element3.attributeValue("id"));
							//writer1.write(element3.attributeValue("id")+"\t");
							//writer2.write(element3.attributeValue("id")+"\t");
							//writer3.write(element3.attributeValue("id")+"\t");
							List list6 = element3.elements("Source");
							Element element4 = (Element) list6.get(0);
							System.out.println("Source:"+ element4.getText());
							if(element4.getText().equals("OMIM")){
								/*
								List<String> ls6 = new ArrayList<String>();
								ls6.add(element4.getText());
								System.out.println("Source1:"+ element4.getText());
								writer1.write(ls6.get(0)+"\t");
								*/
								List list7 = element3.elements("Reference");
								List<String> ls7 = new ArrayList<String>();
								Element element7 = (Element) list7.get(0);
								ls7.add(element7.getText());
								System.out.println("Reference:"+ element7.getText());
								writer1.write(ls7.get(0)+"/");
							}else if(element4.getText().equals("ICD10")){
								/*
								List<String> ls6 = new ArrayList<String>();
								ls6.add(element4.getText());
								System.out.println("Source1:"+ element4.getText());
								writer4.write(ls6.get(0)+"\t");
								*/
								List list7 = element3.elements("Reference");
								List<String> ls7 = new ArrayList<String>();
								Element element7 = (Element) list7.get(0);
								ls7.add(element7.getText());
								System.out.println("Reference:"+ element7.getText());
								writer4.write(ls7.get(0)+"/");
							}else if(element4.getText().equals("MESH")){
								/*
								List<String> ls6 = new ArrayList<String>();
								ls6.add(element4.getText());
								System.out.println("Source:"+ element4.getText());
								writer2.write(ls6.get(0)+"\t");
								*/
								List list7 = element3.elements("Reference");
								List<String> ls7 = new ArrayList<String>();
								Element element7 = (Element) list7.get(0);
								ls7.add(element7.getText());
								System.out.println("Reference:"+ element7.getText());
								writer2.write(ls7.get(0)+"/");
							}else if(element4.getText().equals("UMLS")){
								/*
								List<String> ls6 = new ArrayList<String>();
								ls6.add(element4.getText());
								System.out.println("Source:"+ element4.getText());
								writer3.write(ls6.get(0)+"\t");
								*/
								List list7 = element3.elements("Reference");
								List<String> ls7 = new ArrayList<String>();
								Element element7 = (Element) list7.get(0);
								ls7.add(element7.getText());
								System.out.println("Reference:"+ element7.getText());
								writer3.write(ls7.get(0)+"/");
							}
							
						}
					}
					writer1.write("\r\n");
					writer2.write("\r\n");
					writer3.write("\r\n");
					writer4.write("\r\n");
					writer1.flush();
					writer2.flush();
					writer3.flush();
					writer4.flush();
				}	
			}	
		writer1.close();
		writer2.close();
		writer3.close();
		writer4.close();
		}catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}

猜你在找的XML相关文章