UTF字符串到InputStream的转换
我正在尝试将 XML 数据从 MIDP 设备获取到 Servlet。 Servlet 正在获取数据,如下所示 -
DataInputStream dis = new DataInputStream((InputStream) request.getInputStream());
String readUTF = dis.readUTF();
从 MIDP 完成此操作后,我认为一切都结束了。但现在我在将 readUTF 转换为 InputStream 时遇到问题。我想解析 xml 字符串。我使用了 kXML 2.3.0,这是代码 -
XmlPullParser xpp = new KXmlParser();
try {
xpp.setInput(new InputStreamReader(new ByteArrayInputStream(readUTF.getBytes("UTF-8"))));
int event = xpp.getEventType();
if (event != xpp.END_DOCUMENT) {
System.out.println("Inside Document");
if (xpp.getName() != null) {
System.out.println("The tag is not null");
if (xpp.getName().equals("title")) {
System.out.println("Title =" + xpp.getText());
} else if (xpp.getName().equals("note")) {
System.out.println("Note =" + xpp.getText());
} else if (xpp.getName().equals("priority")) {
System.out.println("Priority =" + xpp.getText());
}
}
event = xpp.next();
}
}
我遇到的问题是只打印了内部文档。这意味着第二个打印语句没有机会执行。我使用 Dom4j 来做同样的事情。
SAXReader sr = new SAXReader();
sr.read(stringReader);
System.out.println(sr.getDocumentFactory().createDocument().asXML());
结果就是这样 -
INFO: <?xml version="1.0" encoding="UTF-8"?>
原始字符串是 - <数据> <任务><标题>dsfsdfds<注释>空<优先级>高 <任务><标题>sdfsdfdsf<注释>空<优先级>高
我验证了它的 XML,它工作得很好。将UTF-8转换为InputStreams有问题吗?还有另一种方法可以将我的 XML 字符串解析为数据吗?
I am trying to get XML data from a MIDP device to a Servlet. Servlet is obtaining the Data as follows -
DataInputStream dis = new DataInputStream((InputStream) request.getInputStream());
String readUTF = dis.readUTF();
After getting this done from MIDP I thought it was over. But now I am having trouble converting the readUTF to a InputStream. I want to parse the xml string. I used kXML 2.3.0 and here is the code -
XmlPullParser xpp = new KXmlParser();
try {
xpp.setInput(new InputStreamReader(new ByteArrayInputStream(readUTF.getBytes("UTF-8"))));
int event = xpp.getEventType();
if (event != xpp.END_DOCUMENT) {
System.out.println("Inside Document");
if (xpp.getName() != null) {
System.out.println("The tag is not null");
if (xpp.getName().equals("title")) {
System.out.println("Title =" + xpp.getText());
} else if (xpp.getName().equals("note")) {
System.out.println("Note =" + xpp.getText());
} else if (xpp.getName().equals("priority")) {
System.out.println("Priority =" + xpp.getText());
}
}
event = xpp.next();
}
}
The problem I am having is that only Inside Document is ever printed. That means that the second print statement doesn't get a chance to execute. I used Dom4j for the same thing.
SAXReader sr = new SAXReader();
sr.read(stringReader);
System.out.println(sr.getDocumentFactory().createDocument().asXML());
The result is something is this just this -
INFO: <?xml version="1.0" encoding="UTF-8"?>
Original string is - <?xml version='1.0' encoding='UTF-8' ?> <data> <task><title>dsfsdfds</title><note>null</note><priority>High</priority></task><task><title>sdfsdfdsf</title><note>null</note><priority>High</priority></task> </data>
I validated XML for it and it works perfectly. Is there a problem for converting UTF-8 to InputStreams? And is there another way to parse my XML String to data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道您的
XmlPullParser
代码,但您的 dom4j 代码已损坏。您已要求 SAXReader 向您提供其文档工厂,然后您要求该新文档,然后打印它。换句话说,这与从 stringReader 读取的数据无关。您忽略了read
调用的结果。请尝试以下操作:查看是否显示完整的 XML。
我注意到,在
XmlPullParser
代码中,您没有像平常那样循环 - 您只是读取第一个节点,检查它,然后重新分配event
的值- 然后不做任何其他事情。I don't know about your
XmlPullParser
code, but your dom4j code is broken. You've asked the SAXReader to give you its document factory, which you're then asking for a new document, and then you're printing it. In other words, that has nothing to do with the data that's been read fromstringReader
. You're ignoring the result of theread
call. Try this instead:See whether that shows the complete XML.
I note that in the
XmlPullParser
code you're not looping round as you normally would - you're only reading the first node, examining it, then reassigning the value ofevent
- and then not doing anything else.