2017-12-29 16 views
0

私は最初の2つのサービス作成者の名前を取得したいと思います。JavaはXMLノードを取得

私は、次のXMLの例があります。

<ServiceSchema:id>10</ServiceSchema:id> 
<ServiceSchema:name>Service 10</ServiceSchema:name> 
<ServiceSchema:authorInfos> 
    <ServiceSchema:authorInfo> 
     <ServiceSchema:id>1</ServiceSchema:id> 
     <ServiceSchema:name>Service Author 1</ServiceSchema:name> 
    <ServiceSchema:authorInfo> 
    <ServiceSchema:authorInfo> 
     <ServiceSchema:id>2</ServiceSchema:id> 
     <ServiceSchema:name>Service Author 2</ServiceSchema:name> 
    <ServiceSchema:authorInfo> 
</ServiceSchema:authorInfos> 

<ServiceSchema:requirements> 
    <ServiceSchema:requirement> 
     <ServiceSchema:id>1</ServiceSchema:id> 
     <ServiceSchema:name>Requirement 1</ServiceSchema:name> 
     <ServiceSchema:authorInfos> 
      <ServiceSchema:authorInfo> 
       <ServiceSchema:id>5</ServiceSchema:id> 
       <ServiceSchema:name> Requirement Author 5</ServiceSchema:name> 
      <ServiceSchema:authorInfo> 
     </ServiceSchema:authorInfos> 
    </ServiceSchema:requirement> 
..... 

私は次のコードで、すべての名前のノードを取得することができます。

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputSource inputSource = new InputSource(new ByteArrayInputStream (xml.getBytes("utf-8"))); 
Document xmldocument = db.parse(inputSource); 
NodeList nodeList = xmldocument.getElementsByTagNameNS("*", "name"); 

しかし、私はの名前だけを取得する方法がわかりません外部の著者。

ありがとうございます。

更新:

私は完全にやろうとしています何がある: 私は異なるノードごとに異なるXMLファイルを検索します。上のxmlファイルは例として意味しました。例えば

searchElementsXml = new String[]{"name", "id", "version", "description", "keywords", "authorInfos.authorInfo.name", "status"}; 

私は多くの値が発生する可能性がありますいくつかのフィールドを持っています。著者情報のように。

だから私のようなものが必要です:

NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos:authorinfo:name"); 

がそこに良い解決策はありますか?

+0

です。 – MrSmith42

答えて

0

あなたが望むものを正しく理解していれば、すべての作者の情報(ServiceSchema:authorInfo)を取得し、各authorInfoの名前ノードを取得することをお勧めします。

+0

私は質問を更新しました。あなたがもう一度それを見ることができればいいと思います。 – Simon

+0

XPATHを使用する必要があります。このチュートリアルを参照してください:http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/ – Ananta

0

あなたのXMLは正しいですか?ServiceSchema:authorInfoタグが閉じることを参照してください。
あなたが探している内容を更新してください。 authorsを取得するには、以下のコードを使用してください。 NodeList nodes = doc.getElementsByTagName( "ServiceSchema:authorInfos");

彼女はあなたが出力ノードとして期待する正確に何を追加してください詳細実行コード

File fXmlFile = new File("/Users/Downloads/test.xml"); 
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(fXmlFile); 
doc.getDocumentElement().normalize(); 
NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos"); 
for (int temp = 0; temp < nodes.getLength(); temp++) { 
    Node nNode = nodes.item(temp); 
    System.out.println(" Current Author Element :" + nNode.getNodeName()); 
    if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
     Element eElement = (Element) nNode; 
     System.out.println("Author Info Name : " + eElement.getElementsByTagName("ServiceSchema:authorInfo").getLength()); 
    } 
} 
+0

私は質問を更新しました。あなたがもう一度それを見ることができればいいと思います。 – Simon

関連する問題