1

JavaでDOMに問題があります...XMLコードは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<bib>
<domain>
    <title>Specifications</title>
    <bib_ref>
        <year>March 2000</year>
        <title>MOF  1.3</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\MOF1_3.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>August 2002</year>
        <title>IDLto Java LanguageMapping Specification</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\IDL2Java.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>1999</year>
        <title>XML Metadata Interchange (XMI) Version 1.1</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\xmi-1.1.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>XML Metadata Interchange (XMI) Version 2</title>
        <author>"OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\XMI2.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>XMI Version 1Production of XML Schema Specification</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\XMI1XSD.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>2002</year>
        <title>EDOC</title>
        <author>OMG</author>
        <weblink>D:\SALIM\Docs\Specifications\EDOC02-02-05.pdf</weblink>
    </bib_ref>
</domain>
<domain>
    <title>Theses</title>
    <bib_ref>
        <year>Octobre 2001</year>
        <title>Echanges de Spécifications Hétérogènes et Réparties</title>
        <author>Xavier Blanc</author>
        <weblink>D:\SALIM\Docs\Theses\TheseXavier.pdf</weblink>
    </bib_ref>
    <bib_ref>
        <year>Janvier 2001</year>
        <title>Composition of Object-Oriented Software Design Models</title>
        <author>Siobhan Clarke</author>
        <weblink>D:\SALIM\Docs\Theses\SClarkeThesis.pdf</weblink>
    </bib_ref>
 ......
 ......

その後、Javaのmain関数で、そこにあるdispContent関数を呼び出します。

public void dispContent (Node n) 
{
    
    String domainName = null;
    
    // we are in an element node
    if (n instanceof Element) {
        Element e = ((Element) n);
       
        // domain title
        if (e.getTagName().equals("title") && e.getParentNode().getNodeName().equals("domain")) {
            domainName = e.getTextContent();
            DomaineTemplate(domainName);
        }
        
        else if (e.getTagName().equals("bib_ref")) {
            NodeList ref = e.getChildNodes();

            for (int i = 0; i < ref.getLength(); i++) {
                Node temp = (Node) ref.item(i);
                
                if (temp.getNodeType() == Node.ELEMENT_NODE) {
                    if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                        continue;
                
                    out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
                }
            }
        }
        
        else {
            NodeList sub = n.getChildNodes();
            for(int i=0; (i < sub.getLength()); i++)
                dispContent(sub.item(i));

        }
    }
        /*else if (n instanceof Document) {
        NodeList fils = n.getChildNodes();
        for(int i=0; (i < fils.getLength()); i++) {
            dispContent(fils.item(i));
    
        }
    }*/
}

「domaineTemplate」関数はそのパラメータを表示するだけです!Javaで「bib_ref」タグを参照すると問題が発生します。「bib_ref」ループごとに、すべての「bib_ref」タグのすべての内容が1行に表示されます。「bib_ref」ごとに1つのコンテンツ(年、タイトル、作成者、およびWebリンクタグ)のみを表示したい。

これが、bib_refを参照したときに表示されているものです。

仕様

年:2000年3月タイトル:MOF 1.3作成者:OMG Webリンク:D:\ SALIM \ Docs \ Specifications \ MOF1_3.pdf年:2002年8月タイトル:IDLto Java LanguageMapping仕様作成者:OMG Webリンク:D:\ SALIM \ Docs \ Specifications \ IDL2Java .pdf年:1999タイトル:XMLメタデータインターチェンジ(XMI)バージョン1.1作成者:OMG Webリンク:D:\ SALIM \ Docs \ Specifications \ xmi-1.1.pdf年:2002タイトル:XMLメタデータインターチェンジ(XMI)バージョン2作成者:OMG weblink:D:\ SALIM \ Docs \ Specifications \ XMI2.pdf年:2002タイトル:XMIバージョン1 XMLスキーマ仕様の作成作成者:OMG weblink:D:\ SALIM \ Docs \ Specifications \ XMI1XSD.pdf年:2002タイトル:EDOC作成者:OMG Webリンク:D:\ SALIM \ Docs \ Specifications \ EDOC02-02-05.pdf

論文

年:Octobre 2001タイトル:EchangesdeSp�cificationsH�t�rog�nesetR�parties著者:Xavier Blancウェブリンク:D:\ SALIM \ Docs \ theses \ theseXavier.pdf年:Janvier 2001タイトル:オブジェクトの構成- Oriented Software Design Models作成者:Siobhan Clarke Webリンク:D:\ SALIM \ Docs \ theses \ SClarkeThesis.pdf年:2002年7月タイトル:寄稿ウェブリンク:D:\ SALIM \ Docs \ Thiss \ ErwanBretonThesis.pdf年:Octobre 2000タイトル:TechniquedeMod�lisationetdeM�tamod�lisation作者:Richard Lemesleウェブリンク:D:\ SALIM \ Docs \ theses\RichardLemesle。 pdf年:Juillet 2002タイトル:Utilsation d'agents mobiles pour la Construction des services distribu�sauthor:Siegfried Rouvrais weblink:D:\ SALIM \ Docs \ theses \ theserouvrais.pdf ... .. ..

手伝って頂けますか ?私はJavaを使用したxmlの初心者であり、約3時間ソリューションを探しています...どうもありがとうございました!

4

2 に答える 2

4

ここdispContent(doc.getFirstChild());で、docは指定されたxmlファイルの内容を含むドキュメントです。

前提条件:改行out.println()を追加System.out.println()DomaineTemplate(domainName);ます(提供された出力に基づく)

コンソールに次のプリントアウトが表示されます。

仕様

年:2000年3月

タイトル:MOF 1.3

著者:OMG

ウェブリンク:D:\ SALIM \ Docs \ Specifications \ MOF1_3.pdf

年:2002年8月

タイトル:IDLtoJavaLanguageMapping仕様

著者:OMG

ウェブリンク:D:\ SALIM \ Docs \ Specifications \ IDL2Java.pdf

年:1999

タイトル:XMLメタデータ交換(XMI)バージョン1.1

著者:OMG

Webリンク:D:\ SALIM \ Docs \ Specifications \ xmi-1.1.pdf

年:2002

タイトル:XMLメタデータ交換(XMI)バージョン2

著者:「OMG

ウェブリンク:D:\ SALIM \ Docs \ Specifications \ XMI2.pdf

年:2002

タイトル:XMIバージョン1XMLスキーマ仕様の作成

著者:OMG

ウェブリンク:D:\ SALIM \ Docs \ Specifications \ XMI1XSD.pdf

年:2002

タイトル:EDOC

著者:OMG

ウェブリンク:D:\ SALIM \ Docs \ Specifications \ EDOC02-02-05.pdf

論文

年:2001年10月

タイトル:EchangesdeSpécificationsHétérogènesetRéparties

著者:ザビエルブラン

ウェブリンク:D:\ SALIM \ Docs \ Thiss \ theseXavier.pdf

年:Janvier 2001

タイトル:オブジェクト指向ソフトウェア設計モデルの構成

著者:シボーン・クラーク

ウェブリンク:D:\ SALIM \ Docs \ theses \ SClarkeThesis.pdf

"\n"新しい行の作成で問題が発生した場合は、システムが使用するものを使用してみてください。

public static final String NEW_LINE = System.getProperty("line.separator");

「bib_ref」ノードの子の各行の間に新しい行を出力したくない場合は、次のように変更します。

else if (e.getTagName().equals("bib_ref")) {
    NodeList ref = e.getChildNodes();

        for (int i = 0; i < ref.getLength(); i++) {
            Node temp = (Node) ref.item(i);

            if (temp.getNodeType() == Node.ELEMENT_NODE) {
                if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                    continue;

                 out.println(temp.getNodeName() + " : " + temp.getTextContent() + "\n");
            }
        }
}

に:

else if (e.getTagName().equals("bib_ref")) {
    NodeList ref = e.getChildNodes();

        for (int i = 0; i < ref.getLength(); i++) {
            Node temp = (Node) ref.item(i);

            if (temp.getNodeType() == Node.ELEMENT_NODE) {
                if (temp.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
                    continue;

                 // Removed "\n":
                 out.println(temp.getNodeName() + " : " + temp.getTextContent());
            }
        }

        // Added out.println();
        out.println();
}

結果:

仕様

年:2000年3月
タイトル:MOF 1.3
著者:OMG
ウェブリンク:D:\ SALIM \ Docs \ Specifications \ MOF1_3.pdf

年:2002年8月
タイトル:IDLtoJavaLanguageMapping仕様
著者:OMG
ウェブリンク:D:\ SALIM \ Docs \ Specifications \ IDL2Java.pdf

年:1999
タイトル:XMLメタデータ交換(XMI)バージョン1.1
著者:OMG
Webリンク:D:\ SALIM \ Docs \ Specifications \ xmi-1.1.pdf

年:2002
タイトル:XMLメタデータ交換(XMI)バージョン2
著者:「OMG
ウェブリンク:D:\ SALIM \ Docs \ Specifications \ XMI2.pdf

年:2002
タイトル:XMIバージョン1XMLスキーマ仕様の作成
著者:OMG
ウェブリンク:D:\ SALIM \ Docs \ Specifications \ XMI1XSD.pdf

年:2002
タイトル:EDOC
著者:OMG
ウェブリンク:D:\ SALIM \ Docs \ Specifications \ EDOC02-02-05.pdf

論文

年:2001年10月
タイトル:EchangesdeSpécificationsHétérogènesetRéparties
著者:ザビエルブラン
ウェブリンク:D:\ SALIM \ Docs \ Thiss \ theseXavier.pdf

年:Janvier 2001
タイトル:オブジェクト指向ソフトウェア設計モデルの構成
著者:シボーン・クラーク
ウェブリンク:D:\ SALIM \ Docs \ theses \ SClarkeThesis.pdf

于 2012-11-29T01:50:42.587 に答える
1

もちろん、質問にhtmlのタグを付けたようですが、これまでのところ、コードはhtmlとは関係ありません。したがって、out.printlnはサーブレットのOutputStreamであり、htmlを出力しようとしていると思います。

したがって、printlnの改行と「\n」の改行はhtmlソースコードでのみ使用できます。ブラウザはこれをスキップします。

この行を変更します

out.println(temp.getNodeName()+ ":" + temp.getTextContent()+ "\ n");

out.println(temp.getNodeName()+ ":" + temp.getTextContent()+ "<br />");

サーブレットは改行で問題なく出力されるはずです。

于 2012-11-29T08:00:58.883 に答える