Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) throws Exception {
- String xml =
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
- "<folder name=\"c\">\n" +
- " <folder name=\"program files\">\n" +
- " <folder name=\"uninstall information\"> \n" +
- " <folder name=\"temporal\" />\n" +
- " <folder name=\"foo\" />\n" +
- " <folder name=\"uninstall information\"> \n" +
- " <folder name=\"temporal\" />\n" +
- " <folder name=\"foo\" />\n" +
- " <folder name=\"uninstall information\"> \n" +
- " <folder name=\"temporal\" />\n" +
- " <folder name=\"foo\" />\n" +
- " <folder name=\"uninstall information\"> \n" +
- " <folder name=\"temporal\" />\n" +
- " <folder name=\"foo\" />\n" +
- " </folder>\n" +
- " </folder>\n" +
- " </folder>\n" +
- " </folder>\n" +
- " </folder>\n" +
- " <folder name=\"fusers\" />\n" +
- "</folder>";
- Collection<String> filter = folderNames(xml, 'f');
- filter.forEach(System.out::println);
- }
- public static Collection<String> folderNames(String xml, char startingLetter) throws Exception {
- String fstPattern = "<folder name=\"";
- List<String> results = new ArrayList<>();
- String[] patterns = xml.split(">");
- for (String p : patterns) {
- int indexof = p.indexOf("=") + 2;
- if (p.contains(fstPattern) && indexof >= 0) {
- int endIndexOF = p.lastIndexOf("\"");
- String result = p.substring(indexof, endIndexOF);
- if(result.charAt(0)==startingLetter){
- results.add(result);
- }
- }
- }
- return results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement