dom4j 解析xml 获取不到根节点
SAXReaderreader=newSAXReader();document=reader.read(newFile("F:/workNeeds/资料/District...
SAXReader reader = new SAXReader();
document = reader.read(new File("F:/workNeeds/资料/Districts.xml"));
// Element rootElm = (Element) document.getRootElement();
Element rootElm = (Element) document.getRootElement();
System.out.println(rootElm.getText()); 展开
document = reader.read(new File("F:/workNeeds/资料/Districts.xml"));
// Element rootElm = (Element) document.getRootElement();
Element rootElm = (Element) document.getRootElement();
System.out.println(rootElm.getText()); 展开
展开全部
import java.io.File;import java.util.Iterator;
import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class Xstream { @SuppressWarnings("rawtypes") public static void xmlPrase(String xml){ try {// Document document=DocumentHelper.parseText(xml);//解析字符串 SAXReader reader = new SAXReader(); //解析xml文件 Document document = reader.read(new File("/test/test.xml"));//解析xml文件
Element rootElt = document.getRootElement(); Iterator bodyIter = rootElt.elementIterator(); while(bodyIter.hasNext()){ Element oe = (Element) bodyIter.next(); if(oe.getName().equals("brank")) System.out.println(oe.getStringValue()); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args){ xmlPrase(""); } //xml文件 如下/* <?xml version="1.0" encoding="UTF-8"?> <root> <brank>a</brank> <brank>b</brank> <brank>c</brank> <brank>d</brank> <brank>e</brank> </root>*/}
获取根节点,System.out.println(rootElm.getName()),不是getTest()
import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class Xstream { @SuppressWarnings("rawtypes") public static void xmlPrase(String xml){ try {// Document document=DocumentHelper.parseText(xml);//解析字符串 SAXReader reader = new SAXReader(); //解析xml文件 Document document = reader.read(new File("/test/test.xml"));//解析xml文件
Element rootElt = document.getRootElement(); Iterator bodyIter = rootElt.elementIterator(); while(bodyIter.hasNext()){ Element oe = (Element) bodyIter.next(); if(oe.getName().equals("brank")) System.out.println(oe.getStringValue()); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args){ xmlPrase(""); } //xml文件 如下/* <?xml version="1.0" encoding="UTF-8"?> <root> <brank>a</brank> <brank>b</brank> <brank>c</brank> <brank>d</brank> <brank>e</brank> </root>*/}
获取根节点,System.out.println(rootElm.getName()),不是getTest()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询