Java 从套接字服务器clojure读取xml输入
我想从套接字解析xml。我已经尝试了许多使用clojure.xml/parse的例子,但还没有突破。下面是我开始使用的示例代码,它只读取基于文本的输入:Java 从套接字服务器clojure读取xml输入,java,xml,sockets,clojure,Java,Xml,Sockets,Clojure,我想从套接字解析xml。我已经尝试了许多使用clojure.xml/parse的例子,但还没有突破。下面是我开始使用的示例代码,它只读取基于文本的输入: (with-open [sock (Socket. host port) reader (io/reader sock) response (StringWriter.)] (while (.isConnected sock) (io/copy reader response)
(with-open [sock (Socket. host port)
reader (io/reader sock)
response (StringWriter.)]
(while (.isConnected sock)
(io/copy reader response)
(log/info response)))
以下是一种方法:
- 有
- 您也可能对以下内容感兴趣:
xmlstr
)。确保可以传输诸如“hello”、“hello There”等字符串。一旦可以将字符读入字符串,就可以解析它们。这是两个独立的问题。我非常确定格式是问题所在,服务器可能正在将请求编码为xml。您对如何接收完整的xml输入有什么见解吗?请使用上面的字符串尝试3个测试。这应该有助于缩小范围。你能为上述内容分享一个套接字示例吗?
(ns tst.clj.core
(:use clj.core
tupelo.test)
(:require
[clojure.java.io :as io]
[net.cgrand.enlive-html :as en-html]
[tupelo.core :as t] ))
(t/refer-tupelo)
; Discard any xml nodes of Type="A" or Type="B" (plus blank string nodes)
(dotest
(let [xml-str "<ROOT>
<Items>
<Item><Type>A</Type><Note>AA1</Note></Item>
<Item><Type>B</Type><Note>BB1</Note></Item>
<Item><Type>C</Type><Note>CC1</Note></Item>
<Item><Type>A</Type><Note>AA2</Note></Item>
</Items>
</ROOT>"
enlive-tree (->> xml-str
java.io.StringReader.
en-html/html-resource
first)]
(spyx-pretty enlive-tree)))
enlive-tree =>
{:tag :ROOT,
:attrs nil,
:content
("\n "
{:tag :Items,
:attrs nil,
:content
("\n "
{:tag :Item,
:attrs nil,
:content
({:tag :Type, :attrs nil, :content ("A")}
{:tag :Note, :attrs nil, :content ("AA1")})}
"\n "
{:tag :Item,
:attrs nil,
:content
({:tag :Type, :attrs nil, :content ("B")}
{:tag :Note, :attrs nil, :content ("BB1")})}
"\n "
{:tag :Item,
:attrs nil,
:content
({:tag :Type, :attrs nil, :content ("C")}
{:tag :Note, :attrs nil, :content ("CC1")})}
"\n "
{:tag :Item,
:attrs nil,
:content
({:tag :Type, :attrs nil, :content ("A")}
{:tag :Note, :attrs nil, :content ("AA2")})}
"\n ")}
"\n ")}