java xml类型_java 解析String类型的xml

网络中有时候我们的参数是以xml类型传递的,有时会把xml拼成一个String字符串。

解析如下:

单一xml文档解析

xml文件

encoding="gb2312"?>

zh

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_r("IfResult").item(0).getFirstChild()!=null){

ifResult=doc.getElementsByTagName_r("IfResult").item(0).getFirstChild().getNodeValue();

if(ifResult.equals("0")){

……

return true;

}else{

if(doc.getElementsByTagName_r("IfWarning").item(0).getFirstChild()!=null){

ifWarning=doc.getElementsByTagName_r("IfWarning").item(0).getFirstChild().getNodeValue();

}

if(doc.getElementsByTagName_r("IfError").item(0).getFirstChild()!=null){

ifError=doc.getElementsByTagName_r("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

xmlInfoParaseModel(String strInfo){

List list=new

ArrayList();

DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

try{

builder = factory.newDocumentBuilder();

Document doc = builder.parse( new

ByteArrayInputStream(strInfo.getBytes()));

int

nodeLength=doc.getElementsByTagName_r("message").getLength();//获取所有节点个数

for(int i=0;i

ExteriorImages images=new ExteriorImages();

images.setR_code(doc.getElementsByTagName_r("code").item(i).getFirstChild().getNodeValue());

images.setBar_state(doc.getElementsByTagName_r("state").item(i).getFirstChild().getNodeValue());

images.setApp_user(doc.getElementsByTagName_r("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;

}

**********************************************************************************************************************************

import java.io.IOException;

import java.io.StringReader;

import java.util.List;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.xml.sax.InputSource;

public class DuXMLDoc {

public List xmlElements(String xmlDoc) {

//创建一个新的字符串

StringReader read = new StringReader(xmlDoc);

//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入

InputSource source = new InputSource(read);

//创建一个新的SAXBuilder

SAXBuilder sb = new SAXBuilder();

try {

//通过输入源构造一个Document

Document doc = sb.build(source);

//取的根元素

Element root = doc.getRootElement();

System.out.println("tasktypename:"+root.getAttributeValue("tasktypename"));

System.out.println("perfrenceNum:"+root.getAttributeValue("perfrenceNum"));

System.out.println(root.getName());//输出根元素的名称(测试)

//得到根元素所有子元素的集合

List jiedian = root.getChildren();

Element et = null;

for(int i=0;i

et = (Element) jiedian.get(i);//循环依次得到子元素

if(et.getAttributeValue("inputindex").equals("1")){

et.setAttribute("name","1");

}

et.setAttribute("age","15");

System.out.println("name:"+et.getAttributeValue("name"));

System.out.println("value:"+et.getAttributeValue("value"));

System.out.println("inputindex:"+et.getAttributeValue("inputindex"));

System.out.println("perfrence:"+et.getAttributeValue("perfrence"));

System.out.println("age:"+et.getAttributeValue("age"));

}

//

// et = (Element) jiedian.get(0);

// List zjiedian = et.getChildren();

// for(int j=0;j

// Element xet = (Element) zjiedian.get(j);

// System.out.println(xet.getName());

// }

} catch (JDOMException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

} catch (IOException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

return null;

}

public static void main(String[] args){

DuXMLDoc doc = new DuXMLDoc();

String xml = "<?xml version=/"1.0/"

encoding=/"UTF-8/"?>"+

"

perfrenceNum=/"2/">"+

"

perfrence=/"2/"/>"+

"

perfrence=/"2/"/>"+

"

"

;

System.out.println(xml);

doc.xmlElements(xml);

}

}

或者试试这个

import javax.xml.parsers.*;

import org.w3c.dom.*;

import java.io.*;

import org.xml.sax.*;

public class DomTest {

public static void main(String args[]) { //建立一个解析器工厂

try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //从解析器工厂生产一个解析器对象

DocumentBuilder builder = factory.newDocumentBuilder(); //解析器对具体的xml文档进行解析,得到文档对象

Document doc = builder.parse("candidate2.xml"); //根据元素的标记名称得到元素列表

NodeList nl = doc.getElementsByTagName_r("PERSON");

for (int i = 0; i < nl.getLength(); i++) {

Element node = (Element) nl.item(i);

System.out.print("NAME: ");

System.out.println(node.getElementsByTagName_r("NAME").item(0).getFirstChild().getNodeValue());

System.out.print("ADDRESS: ");

System.out.println(node.getElementsByTagName_r("ADDRESS").item(0).getFirstChild().getNodeValue());

System.out.print("TEL: ");

System.out.println(node.getElementsByTagName_r("TEL").item(0).getFirstChild().getNodeValue());

System.out.print("FAX: ");

System.out.println(node.getElementsByTagName_r("FAX").item(0).getFirstChild().getNodeValue());

System.out.print("EMAIL: ");

System.out.println(node.getElementsByTagName_r("EMAIL").item(0). getFirstChild().getNodeValue());

System.out.println(); } }

catch (DOMException ex) { }

catch (IOException ex) { }

catch (SAXException ex) { }

catch (ParserConfigurationException ex) { }

catch (FactoryConfigurationError ex) { } } } 用工厂类比较好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值