- - PR -
Node.getNodeValue(); から取得される値について。
1|2|3
次のページへ»
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2004-09-09 21:11
org.w3c.dom のNode.getNodeValue();
を使ってXML から値を取得する処理を作成したのですが、 なにやら最初の1文字しか取得しないケースがあるようです。 ex. <width>100</width> の場合にgetNodeValue() の戻り値が100 ではなく "1" だったり。 この要素の順番を変えたりすると取得できる値が100 になることもあります。 ex. <col name="a"> <width>100</width> </col> <col name="b"> <width>50</width> </col> を <col name="b"> <width>50</width> </col> <col name="a"> <width>100</width> </col> に変えたら正常に値を取得できたりします。 どうして取得できる値が不定なのでしょうか? なぜ最初の1文字しか取得できないことがあるのでしょうか? |
|
投稿日時: 2004-09-10 09:57
追加説明です。 DOMResult result = new DOMResult(); // XMLファイルをXSLTで変換して出力 Transformer#transform( new StreamSource( xmlPath ), result ); // 確認用 - 出力されたXMLをコンソールに出力する Transformer#transform( new StreamSource( xmlPath ), new StreamResult( System.out ) ); 上記処理にて取得データが破壊されます。 2個目のトランスフォームではプロンプトにXML が正常変換された結果が出るのですが、 1個目のトランスフォームではDOM ツリーを見ていくと、この時点でデータが破壊されています。 ありえないとは思うのですが、 javax.xml.transform.Transformer のバグだと思いたいです。。。 どなたか、このような経験、前例はご存知ありませんでしょうか? |
|
投稿日時: 2004-09-10 12:45
「壊れている」というのはどのように判断されましたか?
|
|
投稿日時: 2004-09-10 13:15
uk さん興味を持っていただきありがとうございます。
「壊れている」は eclipse のデバッガにより追加説明部のDOMResult クラスのresult 変数に 格納されているDOM ツリーの構造を追っていき、最初の説明でいう<width>100</width> のElement を探して、そこのvalue を見ました。 ちなみに、いくつかのElementがあるうちの一つだけ、二つだけといった形で壊れます。 ex. <width>100</width> <width>200</width> <width>300</width> <width>400</width> <width>500</width> <width>600</width> の4 番目の値だけ"4" が取得される。等。 この場合、4 番目が壊れるのであって、 <width>400</width> <width>500</width> <width>600</width> <width>100</width> <width>200</width> <width>300</width> 上記のように入れ替えた場合、4 番目の値の100 が壊れる対象となり、"1" が取得されます。 ほかはちゃんと3 桁で取得できます。 ただ、並べ方次第では複数件壊れます。 ??? |
|
投稿日時: 2004-09-10 13:21
できれば、それを確認したソースコードを書いてもらったほうがいいと思います。
場合によってはコンパイル・実行して確認することもできますし。 |
|
投稿日時: 2004-09-10 13:42
まずはjava ソースを。
// TransformerFactoryインスタンスを取得 TransformerFactory tf = TransformerFactory.newInstance(); // XSLファイルからtranceformerを取得 Transformer t = tf.newTransformer( new StreamSource( xsltPath ) ); // 出力するエンコーディングを設定 t.setOutputProperty( ENCODING, SHIFT_JIS ); DOMResult result = new DOMResult(); // XMLファイルをXSLTで変換して出力 t.transform( new StreamSource( xmlPath ), result ); // 確認用 - 出力されたXMLをコンソールに出力する t.transform( new StreamSource( xmlPath ), new StreamResult( System.out ) ); // 結果DOMツリーの取得 Document doc = (Document)result.getNode(); ---- 各xxxPath は各ファイルへのパスです。 |
|
投稿日時: 2004-09-10 13:46
長いですが、xml
<?xml version="1.0" encoding="Shift_JIS"?> <settings> <colmun name="c1"> <table name="t1"/> <display>1</display> <width>90</width> <seq>6</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c2"> <table name="t2"/> <display>1</display> <width>90</width> <seq>4</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c3"> <table name="t3"/> <display>1</display> <width>90</width> <seq>9</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c4"> <table name="t4"/> <display>1</display> <width>90</width> <seq>1</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c5"> <table name="t5"/> <display>1</display> <width>90</width> <seq>2</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c6"> <table name="t6"/> <display>1</display> <width>90</width> <seq>3</seq> <searchdisplay>1</searchdisplay> </colmun> <colmun name="c7"> <table name="t7"/> <display>1</display> <width>90</width> <seq>5</seq> <searchdisplay>1</searchdisplay> </colmun> </settings> |
|
投稿日時: 2004-09-10 13:47
最後にxslt です。
<?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" encoding="Shift_JIS"/> <xsl:template match="/"> <xsl:element name="settings"> <xsl:for-each select="settings/colmun"> <xsl:sort select="seq" data-type="number" order="ascending"/> <xsl:if test="display != 0"> <xsl:element name="colmun"> <xsl:attribute name="name"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:element name="table"> <xsl:attribute name="name"> <xsl:value-of select="table/@name"/> </xsl:attribute> </xsl:element> <xsl:element name="display"> <xsl:value-of select="display"/> </xsl:element> <xsl:element name="width"> <xsl:value-of select="width"/> </xsl:element> <xsl:element name="seq"> <xsl:value-of select="seq"/> </xsl:element> <xsl:element name="searchdisplay"> <xsl:value-of select="searchdisplay"/> </xsl:element> </xsl:element> </xsl:if> </xsl:for-each> </xsl:element> </xsl:template> </xsl:stylesheet> |
1|2|3
次のページへ»