-1

我有一个要求,我将获得一个 xml 文件和一个标签名称作为输入,我必须使用 java.util.xml 使用给定的标签名称拆分 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>

标签名称:书

输出:

<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>
4

2 回答 2

0

这可以很容易地通过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);
    }

}

测试.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>
于 2013-08-28T14:41:16.343 回答
0

我认为一般算法如下:

  • 将文件读入缓冲区
  • 找到您的标签的第一个实例
  • 继续阅读行,直到找到最后一个标签
  • 输出那些行
于 2013-08-28T14:24:02.877 回答