2013-08-28 43 views
-1

我有一個要求中,我會得到一個XML文件&標籤名稱作爲輸入,我用用java給定的標籤名分裂XML文件。 PLS。提示我,那是的分裂大的XML文件成小基於標籤名

輸入: XML文件

<note> 
    <to>Tove</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
    </note> 

    <book> 
    <author>Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <description>An in-depth look at creating applications 
    with XML.</description> 
    </book> 
<book> 
    <author>Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-12-16</publish_date> 
    <description>A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</description> 

標記名稱:本書

OUTPUT:

<book> 
    <author>Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <description>An in-depth look at creating applications 
    with XML.</description> 
    </book> 
<book> 
    <author>Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price>`enter code here` 
    <publish_date>2000-12-16</publish_date> 
    <description>A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</description> 
</book> 
+0

大學課程? – Makky

回答

0

我覺得一般算法如下:

  • 文件讀入到一個緩衝
  • 找到您 標籤的第一個實例
  • 繼續讀行,直到找到最後標籤
  • 輸出 這些線路
0

這可以很容易地完成JSOUP

Jsoup

這裏充滿工作example

import java.io.File; 
import java.io.IOException; 

import org.apache.commons.io.FileUtils; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 

public class Test { 

    public static void main(String args[]) throws IOException { 
     String path = Test.class.getResource("/test.txt").getPath(); 
     String string = FileUtils.readFileToString(new File(path)); 

     Document doc = Jsoup.parse(string); 
     Elements elementsByTag = doc.getElementsByTag("book"); 
     System.out.println(elementsByTag); 
    } 

} 

的test.txt

<note> 
    <to>Tove</to> 
    <from>Jani</from> 
    <heading>Reminder</heading> 
    <body>Don't forget me this weekend!</body> 
    </note> 

    <book> 
    <author>Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <description>An in-depth look at creating applications 
    with XML.</description> 
    </book> 
<book> 
    <author>Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-12-16</publish_date> 
    <description>A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</description> 
    </book> 

輸出

<book> 
<author> 
    Gambardella, Matthew 
</author> 
<title>XML Developer's Guide</title> 
<genre> 
    Computer 
</genre> 
<price> 
    44.95 
</price> 
<publish_date> 
    2000-10-01 
</publish_date> 
<description> 
    An in-depth look at creating applications with XML. 
</description> 
</book> 
<book> 
<author> 
    Ralls, Kim 
</author> 
<title>Midnight Rain</title> 
<genre> 
    Fantasy 
</genre> 
<price> 
    5.95 
</price> 
<publish_date> 
    2000-12-16 
</publish_date> 
<description> 
    A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. 
</description> 
</book>