JDom解析Xml流

TheDaydream 2011-03-23 05:53:31
我从前端传一个xml格式的String到后台,怎么把String转换成xml流? 然后怎么用JDom解析xml流?
String value = "<?xml version='1.0' encoding='GB2312' standalone='yes' ?>" +
"<DATASET version='2.0' dbuser='DS_GDPOST_IBS' dbpassword='a665174bccf656d4d58e7f2ba79c5cf9'>" +
"<SERVICE id='17067620' tablename='ET3D_MAIL_FLOW' action='APPLYUPDATE'>" +
"<COUNT>-1</COUNT>" +
"<PAGE>0</PAGE>" +
"<OPTIONS null='true' />" +
"<COMMANDTEXT />" +
"<SQPARAMS null='true' />" +
"<RETURN />" +
"<DATAPACKET Version='2.0'>" +
"<METADATA>" +
"<FIELDS>" +
"<FIELD attrname='DelegateType' fieldtype='string' with='50'/>" +
"<FIELD attrname='Cbasn' fieldtype='r8' with='50'/>" +
"<FIELD attrname='Cbc' fieldtype='r8' />" +
"<FIELD attrname='OrderDate' fieldtype='r8' />" +
"<FIELD attrname='ClosingDate' fieldtype='string' WIDTH='25' />" +
"<FIELD attrname='SettlementStatus' fieldtype='r8' />" +
"</FIELDS>" +
"<PARAMS DATASET_DELTA='1' />" +
"</METADATA>" +
"<ROWDATA>" +
"<ROW RowState='4' DelegateType='DelegateType' Cbasn='Cbasn' Cbc='Cbc' OrderDate='OrderDate' ClosingDate='ClosingDate' SettlementStatus='SettlementStatus' />" +
"</ROWDATA>" +
"</DATAPACKET>" +
"<SEQ seqname='ET3D_MAIL_FLOW_SEQ' fieldname='BPM_ET3D_MAIL_FLOWID' />" +
"</SERVICE>" +
"</DATASET>'";
以上是我的String内容
...全文
289 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaoweijq 2011-03-24
  • 打赏
  • 举报
回复
stringbufferinputstream已经过时
[Quote=引用 6 楼 zyz1985 的回复:]
//基于字节流
StringBufferInputStream inputStream=new StringBufferInputStream(value);
//基于字符流
StringReader reader=new StringReader(value);

看你要哪个了
[/Quote]
jayqean 2011-03-24
  • 打赏
  • 举报
回复
用prototype
设置下contentType格式 试下吧
游一游走一走 2011-03-24
  • 打赏
  • 举报
回复
//基于字节流
StringBufferInputStream inputStream=new StringBufferInputStream(value);
//基于字符流
StringReader reader=new StringReader(value);

看你要哪个了
TheDaydream 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jayqean 的回复:]
引用 1 楼 chennengqing 的回复:
为什么要定义成String呢?你不能直接将XML文件上传到后台去解析呀?

同问.
String 转成XML,将字符串写到一个XML的文件里,再解析
[/Quote]我用的XmlHttpRequest,这个能提价xml吗? 而且要求不能生成xml物理文件。
yaoweijq 2011-03-24
  • 打赏
  • 举报
回复
java.io.ByteArrayInputStream(String.getBytes());
jayqean 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chennengqing 的回复:]
为什么要定义成String呢?你不能直接将XML文件上传到后台去解析呀?
[/Quote]
同问.
String 转成XML,将字符串写到一个XML的文件里,再解析
TheDaydream 2011-03-24
  • 打赏
  • 举报
回复
问题解决了 原来是String的xml格式有问题,导致在Document document = saxBuilder.build(inputStream); 这段代码的时候出错,因为在转换成Document的时候会验证你的String.
TheDaydream 2011-03-24
  • 打赏
  • 举报
回复
晕.. 这段代码出问题了.. 错误信息如下:
org.jdom.input.JDOMParseException: Error on line 1: Content is not allowed in trailing section.
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:468)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:851)
at com.scce.action.LMISAction.XPathAnalysis(LMISAction.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
TheDaydream 2011-03-24
  • 打赏
  • 举报
回复
恩 已经解决了 我是这样做的
try {
//将字符串转换成输入流
InputStream inputStream = new ByteArrayInputStream(value.getBytes());
//实例化一个SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
//将输入流转换成Document对象
Document document = saxBuilder.build(inputStream);
//使用XPath指定节点位置
XPath xPath = XPath.newInstance("//DATASET/SERVICE/DATAPACKET/METADATA/FIELDS/FIELD");
//获得所有的FIELD节点
List list = xPath.selectNodes(document);
for (int i = 0; i < list.size(); i++) {
Element element = (Element) list.get(i);
Attribute attribute = element.getAttribute("attrname");
String results = attribute.getValue();
System.out.println(results);
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cnqing 2011-03-23
  • 打赏
  • 举报
回复
为什么要定义成String呢?你不能直接将XML文件上传到后台去解析呀?

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧