0

Apache Velocity Apiを使用すると、Java オブジェクト (リスト、POJO など) を (HTML)テンプレートと組み合わせて、(HTML) 出力を作成できます。

これをリバースエンジニアリングするのに役立つJava APIはありますか? この API の入力は、HTML 出力と使用されるテンプレートである可能性があります。出力は、出力の生成に使用されたデータ (Java/XML 形式) である必要があります。

私はHTTP Unit APIを認識していますが、これにより HTML 要素 (テーブルなど) を抽出できます。テンプレートに基づいてデータを抽出するものを探しています。

4

2 に答える 2

1

さまざまなタイプのメッセージを変換するために、google protobuf を使用できます。また、テンプレートの定義も非常に簡単です。JSON.parse() を使用して JavaScript オブジェクトを作成します。Java では、protobuf を使用して JSON を Java オブジェクトに変換できます。

  1. http://code.google.com/p/protobuf/
  2. http://code.google.com/p/protobuf-Java-format/
于 2013-02-11T12:13:23.553 に答える
0

私の答えはおそらくこの質問の作者には役に立たないでしょう(私は5年遅れているので適切なタイミングではないと思います)が、これはGoogleで入力したときに見つけた最初の結果HTML to POJOなので、この回答に出くわす可能性のある他の多くの開発者。

今日、(私の会社の名前で) HTML から POJO への完全なフレームワークをリリースしました。このフレームワークを使用して、HTML を任意の POJO クラスにいくつかの注釈を付けてマップすることができます。ライブラリ自体は非常に便利で、非常にプラグイン可能でありながら、他の多くの機能を備えています。ここでそれを見ることができます:https://github.com/whimtrip/jwht-htmltopojo

使い方:基本

次の html ページを解析する必要があるとします。

<html>
    <head>
        <title>A Simple HTML Document</title>
    </head>
    <body>
        <div class="restaurant">
            <h1>A la bonne Franquette</h1>
            <p>French cuisine restaurant for gourmet of fellow french people</p>
            <div class="location">
                <p>in <span>London</span></p>
            </div>
            <p>Restaurant n*18,190. Ranked 113 out of 1,550 restaurants</p>  
            <div class="meals">
                <div class="meal">
                    <p>Veal Cutlet</p>
                    <p rating-color="green">4.5/5 stars</p>
                    <p>Chef Mr. Frenchie</p>
                </div>

                <div class="meal">
                    <p>Ratatouille</p>
                    <p rating-color="orange">3.6/5 stars</p>
                    <p>Chef Mr. Frenchie and Mme. French-Cuisine</p>
                </div>

            </div> 
        </div>    
    </body>
</html>

マップしたい POJO を作成しましょう。

public class Restaurant {

    @Selector( value = "div.restaurant > h1")
    private String name;

    @Selector( value = "div.restaurant > p:nth-child(2)")
    private String description;

    @Selector( value = "div.restaurant > div:nth-child(3) > p > span")    
    private String location;    

    @Selector( 
        value = "div.restaurant > p:nth-child(4)"
        format = "^Restaurant n\*([0-9,]+). Ranked ([0-9,]+) out of ([0-9,]+) restaurants$",
        indexForRegexPattern = 1,
        useDeserializer = true,
        deserializer = ReplacerDeserializer.class,
        preConvert = true,
        postConvert = false
    )
    // so that the number becomes a valid number as they are shown in this format : 18,190
    @ReplaceWith(value = ",", with = "")
    private Long id;

    @Selector( 
        value = "div.restaurant > p:nth-child(4)"
        format = "^Restaurant n\*([0-9,]+). Ranked ([0-9,]+) out of ([0-9,]+) restaurants$",
        // This time, we want the second regex group and not the first one anymore
        indexForRegexPattern = 2,
        useDeserializer = true,
        deserializer = ReplacerDeserializer.class,
        preConvert = true,
        postConvert = false
    )
    // so that the number becomes a valid number as they are shown in this format : 18,190
    @ReplaceWith(value = ",", with = "")
    private Integer rank;

    @Selector(value = ".meal")    
    private List<Meal> meals;

    // getters and setters

}

そして今、Mealクラスも:

public class Meal {

    @Selector(value = "p:nth-child(1)")
    private String name;

    @Selector(
        value = "p:nth-child(2)",
        format = "^([0-9.]+)\/5 stars$",
        indexForRegexPattern = 1
    )
    private Float stars;

    @Selector(
        value = "p:nth-child(2)",
        // rating-color custom attribute can be used as well
        attr = "rating-color"
    )
    private String ratingColor;

    @Selector(
        value = "p:nth-child(3)"
    )
    private String chefs;

    // getters and setters.
}

github ページで、上記のコードに関する詳細な説明を提供しました。

とりあえず、これをスクラップする方法を見てみましょう。

private static final String MY_HTML_FILE = "my-html-file.html";

public static void main(String[] args) {


    HtmlToPojoEngine htmlToPojoEngine = HtmlToPojoEngine.create();

    HtmlAdapter<Restaurant> adapter = htmlToPojoEngine.adapter(Restaurant.class);

    // If they were several restaurants in the same page, 
    // you would need to create a parent POJO containing
    // a list of Restaurants as shown with the meals here
    Restaurant restaurant = adapter.fromHtml(getHtmlBody());

    // That's it, do some magic now!

}


private static String getHtmlBody() throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(MY_HTML_FILE));
    return new String(encoded, Charset.forName("UTF-8"));

}

別の短い例はここにあります

これが誰かを助けることを願っています!

于 2018-07-25T22:16:11.050 に答える