java 解析String类型的xml
2009-04-13 19:38
381 查看
网络中有时候我们的参数是以xml类型传递的,有时会把xml拼成一个String字符串。
解析如下:
单一xml文档解析
xml文件
<?xml version="1.0" encoding="gb2312"?>
<IfResponse>
<IfResult ></IfResult>
<IfWarning ></IfWarning>
<IfError >zh</IfError>
</IfResponse>
java 解析
public static boolean strParseXML(String str){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
String ifResult=null;
String ifWarning=null;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(str.getBytes()));
if(doc.getElementsByTagName("IfResult").item(0).getFirstChild()!=null){
ifResult=doc.getElementsByTagName("IfResult").item(0).getFirstChild().getNodeValue();
if(ifResult.equals("0")){
……
return true;
}else{
if(doc.getElementsByTagName("IfWarning").item(0).getFirstChild()!=null){
ifWarning=doc.getElementsByTagName("IfWarning").item(0).getFirstChild().getNodeValue();
}
if(doc.getElementsByTagName("IfError").item(0).getFirstChild()!=null){
ifError=doc.getElementsByTagName("IfError").item(0).getFirstChild().getNodeValue();
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
多重参数解析,以对象形式放到List里
public static List<ExteriorImages> xmlInfoParaseModel(String strInfo){
List<ExteriorImages> list=new ArrayList<ExteriorImages>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(strInfo.getBytes()));
int nodeLength=doc.getElementsByTagName("message").getLength();//获取所有节点个数
for(int i=0;i<nodeLength;i++){
ExteriorImages images=new ExteriorImages();
images.setR_code(doc.getElementsByTagName("code").item(i).getFirstChild().getNodeValue());
images.setBar_state(doc.getElementsByTagName("state").item(i).getFirstChild().getNodeValue());
images.setApp_user(doc.getElementsByTagName("user").item(i).getFirstChild().getNodeValue());
list.add(images);
}
return list;
}
catch(ParserConfigurationException e) {
e.printStackTrace();
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
解析如下:
单一xml文档解析
xml文件
<?xml version="1.0" encoding="gb2312"?>
<IfResponse>
<IfResult ></IfResult>
<IfWarning ></IfWarning>
<IfError >zh</IfError>
</IfResponse>
java 解析
public static boolean strParseXML(String str){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
String ifResult=null;
String ifWarning=null;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(str.getBytes()));
if(doc.getElementsByTagName("IfResult").item(0).getFirstChild()!=null){
ifResult=doc.getElementsByTagName("IfResult").item(0).getFirstChild().getNodeValue();
if(ifResult.equals("0")){
……
return true;
}else{
if(doc.getElementsByTagName("IfWarning").item(0).getFirstChild()!=null){
ifWarning=doc.getElementsByTagName("IfWarning").item(0).getFirstChild().getNodeValue();
}
if(doc.getElementsByTagName("IfError").item(0).getFirstChild()!=null){
ifError=doc.getElementsByTagName("IfError").item(0).getFirstChild().getNodeValue();
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
多重参数解析,以对象形式放到List里
public static List<ExteriorImages> xmlInfoParaseModel(String strInfo){
List<ExteriorImages> list=new ArrayList<ExteriorImages>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(strInfo.getBytes()));
int nodeLength=doc.getElementsByTagName("message").getLength();//获取所有节点个数
for(int i=0;i<nodeLength;i++){
ExteriorImages images=new ExteriorImages();
images.setR_code(doc.getElementsByTagName("code").item(i).getFirstChild().getNodeValue());
images.setBar_state(doc.getElementsByTagName("state").item(i).getFirstChild().getNodeValue());
images.setApp_user(doc.getElementsByTagName("user").item(i).getFirstChild().getNodeValue());
list.add(images);
}
return list;
}
catch(ParserConfigurationException e) {
e.printStackTrace();
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
相关文章推荐
- java解析String 类型的xml数据
- java解析String类型t复杂xml,多级节点,最好的例子
- java解析string类型的xml
- java解析String类型t复杂xml,多级节点,最好的例子
- 解析Java中的String对象的数据类型
- java内存分配和String类型的深度解析
- java解析String字符串xml 多级节点NodeList
- 将String类型的XML解析并设置到实体类中
- java中的string类型全面解析
- java内存分配和String类型的深度解析
- java内存分配和String类型的深度解析
- java内存分配和String类型的深度解析
- Java中String类型的参数传递问题的解析
- java解析由String类型拼接的XML文件
- java内存分配和String类型的深度解析
- java内存分配和String类型的深度解析
- 全面解析Java中的String数据类型
- java内存分配和String类型的深度解析
- 通过递归的方式解析String 字符串类型的XML并保存进实体
- java如何解析json字符串(List<Map<String,Object>成功的类型)