English 中文(简体)
如何通过 Java的XML档案进行搜索,然后制作XML档案?
原标题:How do i search through an XML file in Java, and then output the XML files names?
  • 时间:2011-11-23 09:51:01
  •  标签:
  • java
  • xml

我发现过与地雷类似的问题,但并非完全一样。

你们看到,我试图做的是,通过一系列的XML档案在名录中搜寻,并告诉我Xml档案中包含着某种星体。

例如:一搜寻“性别与知识”,该方案将告诉我“4.xml”,因为XML档案中有书面“性别与知识”,即找到了查明所有XML档案的途径,但现在需要一些能够通过这些XML档案检索并授予载有我方言的XML档案标题的法典。

定义准则一如上:

String path = "C:/Users/Daniel/Desktop/CD";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
System.out.println(listOfFiles.length);

最佳回答

Raw File IO

这种方法没有考虑到任何XML标记或任何东西。 它只是对档案中所含的某种扼杀进行检查。

你们必须采取的基本步骤是使用光线文件:

  1. Loop through the files in listOfFiles
  2. Open each file (see FileReader and BufferedReader)
  3. Read the content (e.g. line by line with readLine)
  4. Check if your string is in the read content
  5. If yes, output the file s name and continue with next file

或以最低代码表示:

// In looping through files:
// Step 2:
BufferedReader r = new BufferedReader(new FileReader(f)); 
// Step 3:
String s = r.readLine();
while (s != null) {
    // Step 4:
    if (s.contains("TestString")) {
        // Step 5:
        System.out.println(f.getAbsolutePath());
        break;
    }
    s = r.readLine();
}
r.close();

请注意,如果你扼杀有线性的话,这就赢得了工作。

SAX2

SAX是一种面向上游的XML教区标准,其行文为XML,如果某一要素开始、案文找到等,则向手提退。 您可以利用这一方法核对《国际禁止杀伤人员地雷公约》的案文内容,并且还执行一部国家文书,如果文本确实在<代码>和“t”中碰到,则跟踪该文本;要素,但这是一个比较复杂的问题。

This is what to do using a SAX2 implementation:

  1. Implement the ContentHandler SAX interface in your class
  2. In that content handler, implement the characters method to check for your string
  3. Create a reader with XMLReaderFactory.createXMLReader()
  4. Set your content handler with setContentHandler on the reader
  5. Call parse on the reader with an InputSource for your file

DOM

DOM is a standard XML model that builds a tree of the nodes in memory that can be traversed. This method is really simple to use:

  1. Parse the XML into a DOM using DocumentBuilder
  2. Get all <string> elements using getElementsByTagName
  3. Check if their values equal your test string using getNodeValue

Code:

Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
NodeList l = d.getElementsByTagName("string");
for (int i = 0; i < l.getLength(); ++i) {
    if ("TestString".equals(l.item(i).getNodeValue())) {
        System.out.println(f.getAbsolutePath());
        break;
    }
}

希望这一帮助。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签