Javaを使用してXMLノードの属性値を取得する方法

プリヤ:

私は次のようなxmlを持っています:

{ <xml><ep><source type="xml">...</source><source type="text">..</source></ep></xml>}

ここで、タイプが属性である「ソースタイプ」の値を取得したい。

私はこのようにしようとしましたが、うまくいきません:

 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                try {
                    DocumentBuilder builder = domFactory.newDocumentBuilder();
                    Document dDoc = builder.parse("D:/workspace1/ereader/src/main/webapp/configurations/config.xml");
                    System.out.println(dDoc);
                    XPath xPath = XPathFactory.newInstance().newXPath();
                    Node node = (Node) xPath.evaluate("//xml/source/@type/text()", dDoc, XPathConstants.NODE);
                    System.out.println(node);
                } catch (Exception e) {
                    e.printStackTrace();

私もこれを試しました:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader("config.xml"));
            Document doc = builder.parse(is);

            NodeList nodeList = doc.getElementsByTagName("source");

            for (int i = 0; i < nodeList.getLength(); i++) {                
                Node node = nodeList.item(i);

                if (node.hasAttributes()) {
                    Attr attr = (Attr) node.getAttributes().getNamedItem("type");
                    if (attr != null) {
                        String attribute= attr.getValue();                      
                        System.out.println("attribute: " + attribute);                      
                    }
                }
            }

plsは私を助けます!!

Varsha、事前に感謝します。

gks:

あなたの質問はより一般的なので、Javaで利用可能なXMLパーサーで実装してみてください。パーサーに固有の質問が必要な場合は、ここでコードを更新してください。

<?xml version="1.0" encoding="UTF-8"?>
<ep>
    <source type="xml">TEST</source>
    <source type="text"></source>
</ep>
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("uri to xmlfile");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//ep/source[@type]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

for (int i = 0; i < nl.getLength(); i++)
{
    Node currentItem = nl.item(i);
    String key = currentItem.getAttributes().getNamedItem("type").getNodeValue();
    System.out.println(key);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

TOP 一覧

  1. 1

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  2. 2

    Ansibleで複数行のシェルスクリプトを実行する方法

  3. 3

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  4. 4

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  5. 5

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  6. 6

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  7. 7

    パンダは異なる名前の列に追加します

  8. 8

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  9. 9

    グラフ(.PNG)ファイルをエクスポートするZabbix

  10. 10

    Chromeウェブアプリのウェブビューの高さの問題

  11. 11

    ラベルとエントリがpythontkinterに表示されないのはなぜですか?

  12. 12

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  13. 13

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  14. 14

    Reactでclsxを使用する方法

  15. 15

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  16. 16

    テキストフィールドの値に基づいて UIslider を移動します

  17. 17

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  20. 20

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  21. 21

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

ホットタグ

アーカイブ