2017-07-20 4 views
0

再帰メソッドを使用して、XMLファイルのトピック(ヘルプ&マニュアル以外のもの)を1つずつ読み込もうとしています。再帰メソッドを使用してXMLからノードを取得する(Javaの場合)

残念ながら、プログラマーは私が(何も印刷しない)したいと思っているようなトピックを印刷せず、stackoverflowに詰まってしまうので、間違いがあるようです。 私はそれを見つけることはできません..

私は助けていただきありがとうございます!

EDIT:私はデバッグでいくつかの時間を費やしましたが、再帰そのもの以外の問題の1つは、topicref(それは属性)の間違った「ノード」を読み取っていることです。私はそれをキャプションを読むようにする方法を知らない。 +それは1つのtopicrefを作成するだけで、それは実行されません。

コード:

 package org.joox; 

     import java.io.IOException; 

     import javax.xml.parsers.DocumentBuilder; 
     import javax.xml.parsers.DocumentBuilderFactory; 
     import javax.xml.parsers.ParserConfigurationException; 

     import org.w3c.dom.Document; 
     import org.w3c.dom.Node; 
     import org.w3c.dom.NodeList; 
     import org.xml.sax.SAXException; 

     public class XPathDemo { 
     DocumentBuilderFactory factory = 
     DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     Document doc = null; 

    public static void main(String[] args) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     DocumentBuilder builder; 
     Document doc = null; 
     try { 
      builder = factory.newDocumentBuilder(); 
      doc = builder.parse("table_of_contents.xml"); 

      GiveCaption(doc.getChildNodes().item(0)); 
     } catch (ParserConfigurationException | SAXException | IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    private static void GiveCaption(Node n) { 
     // no child nodes + no caption -> end 
     if (!n.hasChildNodes() && !n.getNodeName().equals("caption")) { 
      return; 
     } 
     NodeList nodes = n.getChildNodes(); 

     if (n.getChildNodes().getLength() == 0) 

     { 
      return; 
     } 
     if (n.getNodeName().equals("topicref")) { 
      if (n.getNodeName().equals("caption")) { 
       System.out.println(n); 
      } 
     } 

     for (int i = 0; i < nodes.getLength(); i++) { 
     n = nodes.item(i); 
     } 
     for (int i = 0; i < nodes.getLength(); i++) { 
      GiveCaption(n); 
     } 


    } 
} 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<map xmlns:xsi="http://www.w3.org/2001/XInclude"> 
    <topicref type="topic" id="429303521027079" build="ALL" modified="2017-07-14T10:50:14.916Z" icon="0" href="Introduction"> 
    <caption translate="true">Introduction</caption> 
    <topicref type="topic" id="429305694503733" build="ALL" modified="2017-07-14T10:50:15.258Z" icon="0" href="Welcome-topic"> 
     <caption translate="true">Welcome topic</caption> 
    </topicref> 
    <topicref type="topic" id="42930890558253" build="ALL" modified="2017-07-14T10:50:15.479Z" icon="0" href="Second-topic"> 
     <caption translate="true">Second topic</caption> 
    </topicref> 
    </topicref> 
    <topicref type="topic" id="429303877549160" build="ALL" modified="2017-07-14T10:50:15.711Z" icon="0" href="Chapter-2"> 
    <caption translate="true">Chapter 2</caption> 
    <topicref type="topic" id="429304160418503" build="ALL" modified="2017-07-14T10:50:15.937Z" icon="0" href="Overview"> 
     <caption translate="true">Overview</caption> 
    </topicref> 
    <topicref type="topic" id="429304436298052" build="ALL" modified="2017-07-14T10:50:16.168Z" icon="0" href="Sub-chapter-2_1"> 
     <caption translate="true">Sub chapter 2.1</caption> 
     <topicref type="topic" id="429302637318652" build="ALL" modified="2017-07-14T10:50:16.395Z" icon="0" href="New-topic"> 
     <caption translate="true">New topic</caption> 
     </topicref> 
    </topicref> 
    </topicref> 
</map> 
+3

[最小限で完全で検証可能な例](http://stackoverflow.com/help/mcve)の精神で、無関係なコードをすべて削除してください。 – kazemakase

+0

完了。私は新しいです申し訳ありません:) – Raphael

+0

技術を変更する場合は、XPathを使用してください。すべてのキャプションノードを取得するには、 '//caption'を使うだけです。 – beat

答えて

0

私は、これは直接あなたの質問に答えていない知っているが、あなたは自分自身に多くの困難を救うことができるとすぐにJAXBを始めます。 http://www.vogella.com/tutorials/JAXB/article.html

JAXBは、JavaオブジェクトをXMLからとに を変換する方法を定義するJava標準です。標準のマッピングセットを使用します。

JAXBが適切でない可能性がある唯一の理由は、非常に大きなXMLファイルを読み込み、コンテンツをストリーミングして+処理をその場で実行したい場合です。

この場合でも、STAX(新しいJava XMLライブラリ)はSAXよりも使いやすいです。 http://www.vogella.com/tutorials/JavaXML/article.html#javastax

+0

お返事ありがとうございます。私はそれをチェックし、私はそれのための任意の使用を見つけることができるかどうかを確認します。 – Raphael

0

コードは、操作の一般的な流れを乱していた以前の実験のビットがあるようです。このコードと元のコードの主な変更点は、GiveCaptionメソッドのいくつかの要素の削除です。

はじめ
ようこそトピック
セカンドトピック
:これは、出力を生成

import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

public class XPathDemo { 

    public static void main(String[] args) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     DocumentBuilder builder; 
     Document doc = null; 
     try { 
      builder = factory.newDocumentBuilder(); 
      doc = builder.parse("table_of_contents.xml"); 

      giveCaption(doc.getChildNodes().item(0)); 
     } catch (ParserConfigurationException | SAXException | IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private static void giveCaption(Node n) { 
     NodeList nodes = n.getChildNodes(); 

     if (n.getNodeName().equals("caption")) { 
      System.out.println(n.getFirstChild().getNodeValue()); 
     } 

     for (int i = 0; i < nodes.getLength(); i++) { 
      giveCaption(nodes.item(i)); 
     } 
    } 
} 

:標準の命名規則は、小文字で始まるメソッドを持っているのでところで、私はまた、その変更を行いました 第2章
概要
サブ章2.1
新しいトピック

関連する問題