java 解析xml文件内容?
<infos><info><request>sss</request><response>{aa:bb}</response></info><info><request>...
<infos>
<info>
<request>sss</request>
<response>
{aa:bb}
</response>
</info>
<info>
<request>qqq</request>
<response>
{aa:bb}
</response>
</info>
</infos>
根据request 取出response的值
可能多个info
请大神 把代码贴上 最好有注解 从别地复制过来的只要好用就OK
<?xml version="1.0" encoding="UTF-8"?>
<infos>
<info>
<request>sss</request>
<response>
{aa:bb}
</response>
</info>
<info>
<request>qqq</request>
<response>
{aa:bb}
</response>
</info>
</infos>
</xml> 展开
<info>
<request>sss</request>
<response>
{aa:bb}
</response>
</info>
<info>
<request>qqq</request>
<response>
{aa:bb}
</response>
</info>
</infos>
根据request 取出response的值
可能多个info
请大神 把代码贴上 最好有注解 从别地复制过来的只要好用就OK
<?xml version="1.0" encoding="UTF-8"?>
<infos>
<info>
<request>sss</request>
<response>
{aa:bb}
</response>
</info>
<info>
<request>qqq</request>
<response>
{aa:bb}
</response>
</info>
</infos>
</xml> 展开
展开全部
public class Context {
private String request ;
private String response ;
public String getRequest() {
return request;
}
public void setRequest(String request) {
this.request = request;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class XmlInfoUtils {
public static void parseXML(String xml) throws Exception{
//将xml转换为document对象
Document document = DocumentHelper.parseText(xml);
//获取根目录节点 infos ;
Element root = document.getRootElement() ;
//获取根节点下的所有直接 子节点info
List<Element> infoNodes = root.elements();
//遍历所有info节点
for(Element element : infoNodes){
//获取info节点下的所有直接子节点 request 和 response
List<Element> contextNodes = element.elements() ;
//遍历节点
for(Element context : contextNodes){
//获取值
System.out.println(context.getName() +" "+context.getText());
}
}
}
/**
* 生成所需要的XML
*/
public static String createXML(List<Context> list) throws Exception{
Document document = DocumentHelper.createDocument() ;
Element root = document.addElement("infos");
for(Context context : list){
Element e = root.addElement("info");
e.addElement("request").setText(context.getRequest());
e.addElement("response").setText(context.getResponse());
}
return document.asXML();
}
/**测试代码*/
public static void main(String[] args) throws Exception{
//准备数据
List<Context> list = new ArrayList<Context>();
Context context = new Context();
context.setRequest("sss");
context.setResponse("{aa:bb}");
list.add(context);
context = new Context();
context.setRequest("qqq");
context.setResponse("{aa:bb}");
list.add(context);
//创建XML
String xml = createXML(list) ;
//解析XML
parseXML(xml);
}
}
展开全部
还是用正则表达式吧。解析保存至map。然后根据key得到value
package com.thinkland.test;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TTT {
public static Pattern p_info=Pattern.compile("<info>.*?</info>",Pattern.DOTALL);
public static Pattern p_req=Pattern.compile("<request>.*?</request>");
public static Pattern p_res=Pattern.compile("<response>.*?</response>");
public static Map<String,String> maps=new HashMap<String, String>();
public static void main(String[] args) {
String str = "<infos> <info> <request>sss</request> <response> {aa:bb} </response> </info> <info> <request>qqq</request> <response> {aa:bb} </response> </info></infos>";
Matcher m=p_info.matcher(str);
while(m.find()){
String info=m.group();
Matcher m2=p_req.matcher(info);
String req=null,res=null;
if(m2.find()){
req=m2.group();
req=req.substring(9,req.length()-10);
}
m2=p_res.matcher(info);
if(m2.find()){
res=m2.group();
res=res.substring(10,res.length()-11);
}
maps.put(req, res);
}
System.out.println(maps.get("sss"));
}
}
package com.thinkland.test;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TTT {
public static Pattern p_info=Pattern.compile("<info>.*?</info>",Pattern.DOTALL);
public static Pattern p_req=Pattern.compile("<request>.*?</request>");
public static Pattern p_res=Pattern.compile("<response>.*?</response>");
public static Map<String,String> maps=new HashMap<String, String>();
public static void main(String[] args) {
String str = "<infos> <info> <request>sss</request> <response> {aa:bb} </response> </info> <info> <request>qqq</request> <response> {aa:bb} </response> </info></infos>";
Matcher m=p_info.matcher(str);
while(m.find()){
String info=m.group();
Matcher m2=p_req.matcher(info);
String req=null,res=null;
if(m2.find()){
req=m2.group();
req=req.substring(9,req.length()-10);
}
m2=p_res.matcher(info);
if(m2.find()){
res=m2.group();
res=res.substring(10,res.length()-11);
}
maps.put(req, res);
}
System.out.println(maps.get("sss"));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用DOM4J技术可以实现,你网上百度下,也有现成的代码,拷贝过来改改就行了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用XStream,搜索下,你下载它依赖的jar包,非常好用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询