2017-06-09 19 views
0

xmlをソケットから解析します。私はclojure.xml/parseを使って多くの例を試しましたが、まだブレークスルーはありません。ここでソケットサーバーからxml入力を読み込みます。

(with-open [sock (Socket. host port) 
      reader (io/reader sock) 
      response (StringWriter.)] 
    (while (.isConnected sock) 
    (io/copy reader response) 
    (log/info response))) 
+0

あなたがソケットから何か(テキスト文字列)を読み取ることができますに興味がある可能性があり

  • ?または、解析とXMLストリングの問題がclojureデータ構造にあるのでしょうか? –

  • +0

    はい、私はソケットからテキストを読むことができますが、これは私が入手したものです:

    +1

    ソケット設定に何か間違っているはずです。 XMLは文字列内の単なる文字です(この例では 'xml-str'のように)。 "hello"、 ""、 "こんにちは、"などの文字列を送信できることを確認してください。文字列を文字列に読み込むと、その文字列を解析できます。それは2つの別々の問題です。 –

    答えて

    0

    doing it using Enliveの一つの方法である:以下は、私はそれを開始したサンプルコードは、テキストベースの入力を読み取る

    (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     ")} 
    
    +0

    上記のソケットの例を共有できますか? –

    関連する問題