讲XML转换成document(Element)

datas.xml文件的内容

<return>
<user>
<userID>U0IZA8XVL0VLMJ2ZR2UTBM9CRT-HF1</userID>
<email>[email protected]</email>
<displayName>vic2 wang2</displayName>
</user>
<user><userID>U610J38PBDYGTTHVYCRI0OB4MU-HF1</userID>
<email>[email protected]</email>
<displayName>[email protected]</displayName>
</user>
</return>

 现在要将其转换为Document 或者Element

(其实我需要将其转换成Element,但是由于对dom4j的API不熟悉,搞了好久才搞明白)

 

import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test {
	public static void main(String args[]) throws DocumentException {
		SAXReader reader;
		Document document;
		File xmlFile;
               //xml的所在的路径
		xmlFile = new File("D:/datas.xml");
		reader = new SAXReader();
               //转换成document
		document = reader.read(xmlFile);
              //获取根元素,即获得所有的Node
		Element rootElement = document.getRootElement();
		System.out.println(rootElement.asXML());
	}
}

 其实很容易,哎,困惑了我好久。没文化太可怕啊。

 

 

 

 

你可能感兴趣的:(xml)