JAVA利用Webservice从对方提供的接口获取XML格式的数据
1个回答
展开全部
//dom4j解析XML
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("student2.xml"));
Element root = doc.getRootElement();
List childList = root.elements();
List childList2 = root.elements("hello");
Element first = root.element("hello");
System.out.println(first.attributeValue("age"));
for(Iterator iter = root.elementIterator(); iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("age"));
}
System.out.println("---------------------------");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(new File("student2.xml"));
DOMReader domReader = new DOMReader();
//将JAXP的Document转换为dom4j的Document
Document d = domReader.read(document);
Element rootElement = d.getRootElement();
System.out.println(rootElement.getName());
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("student2.xml"));
Element root = doc.getRootElement();
List childList = root.elements();
List childList2 = root.elements("hello");
Element first = root.element("hello");
System.out.println(first.attributeValue("age"));
for(Iterator iter = root.elementIterator(); iter.hasNext();)
{
Element e = (Element)iter.next();
System.out.println(e.attributeValue("age"));
}
System.out.println("---------------------------");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(new File("student2.xml"));
DOMReader domReader = new DOMReader();
//将JAXP的Document转换为dom4j的Document
Document d = domReader.read(document);
Element rootElement = d.getRootElement();
System.out.println(rootElement.getName());
更多追问追答
追问
你有没有这方面的编程经验啊,可以加球球教教我吗?,还是有不懂啊,求指教
追答
上面和下面是两种获取根元素的方法,然后根据根元素一步一步获取元素对应的value就行了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询