タグ及びCXタグの要素の内容を編集しよう!

※サンプルでは、HTMLを使用していますが、XHTMLでも同じです。

(1) 要素の内容を変更する。

下記のHTMLの赤字の部分を動的に変更してみます。"Hello,World"を"こんにちは、世界!"に変更します。

element1.html
<html>
<head>
<title>要素の内容を編集しよう!</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<font id="hello">Hello,World</font>
</body>
</html>


Meteorを使用して作成した場合、以下のようなコードになります。

ElementServlet1.java

//標準API

import java.io.*;

//ServletAPI
import javax.servlet.*;
import javax.servlet.http.*;

//Kuro API
import jp.kuro.meteor.*;
import jp.kuro.meteor.printer.HttpPrinter;

public class ElementServlet1 extends HttpServlet {
    ParserFactory pf;

    public void init(ServletConfig sConf) throws ServletException {
        //"element1.html"の絶対パスを取得する
        ServletContext sc = sConf.getServletContext();
        String path = sc.getRealPath("/WEB-INF/html/");

        //パーサファクトリオブジェクトを生成し、"element1.html"を読み込む
        pf = new ParserFactory(path);
        pf.parser(Parser.HTML, "element1.html", "Shift_JIS");
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //Parserオブジェクトを取得する
        Parser xt = pf.parser("element1");
        //"Hello,World"を"こんにちは、世界!"に変更します。
        Element tag = xt.element("font", "id", "hello");
        tag.content("こんにちは、世界");
        //反映する
        xt.flush();
        //HTTP出力する
        HttpPrinter prt = new HttpPrinter(res);
        prt.print(xt);
    }
}



(2)CXタグの要素の内容を変更する

下記のHTMLの赤字の部分を動的に変更してみます。"Hello,World"を"こんにちは、世界!"に変更します。

element2.html
<html>
<head>
<title>CXタグの要素の内容を編集しよう!</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<!-- @quark id="hello" -->Hello,World<!-- /@quark -->
</body>
</html>


Meteorを使用して作成した場合、以下のようなコードになります。

ElementServlet2.java

//標準API

import java.io.*;

//ServletAPI
import javax.servlet.*;
import javax.servlet.http.*;

//Kuro API
import jp.kuro.meteor.*;
import jp.kuro.meteor.printer.HttpPrinter;

public class ElementServlet2 extends HttpServlet {
    ParserFactory pf;

    public void init(ServletConfig sConf) throws ServletException {
        //"element2.html"の絶対パスを取得する
        ServletContext sc = sConf.getServletContext();
        String path = sc.getRealPath("/WEB-INF/html/);

        //パーサファクトリオブジェクトを生成し、"element2.html"を読み込む
        pf = new ParserFactory(path);
        pf.parser(Parser.HTML, "element2.html", "Shift_JIS");
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //Parserオブジェクトを取得する
        Parser xt = pf.parser();
        //fontタグのcolor属性を消します。
        Element tag = xt.cxTag("hello");
        tag.content("こんにちは、世界!");
        //反映する
        xt.flush();
        //HTTP出力する
        HttpPrinter prt = new HttpPrinter(res);
        prt.print(xt);
    }
}




(C)Yasumasa Ashida