Created
January 13, 2018 08:57
-
-
Save Generalus/716cc96c39cf7bfb7a83a9c52d551c71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlAccessType; | |
import javax.xml.bind.annotation.XmlAccessorType; | |
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import java.util.ArrayList; | |
import java.util.stream.Stream; | |
@XmlAccessorType(XmlAccessType.FIELD) | |
@XmlRootElement(name = "folder") | |
public class Folder { | |
@XmlAttribute(name = "name") | |
private String name; | |
@XmlElement(name = "folder") | |
private ArrayList<Folder> folderList; | |
public String getName() { | |
return name; | |
} | |
public ArrayList<Folder> getFoldersList() { | |
return folderList; | |
} | |
public Stream<Folder> extractTree() { | |
return folderList == null ? | |
Stream.of(this) : | |
Stream.concat( | |
Stream.of(this), | |
folderList.stream().flatMap(Folder::extractTree) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Unmarshaller; | |
import java.io.ByteArrayInputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Collection; | |
import java.util.stream.Collectors; | |
public class Main { | |
public static Collection<String> folderNames(String xml, char startingLetter) { | |
try { | |
JAXBContext jaxbContext = JAXBContext.newInstance(Folder.class); | |
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); | |
Folder folder = (Folder) jaxbUnmarshaller.unmarshal( | |
new ByteArrayInputStream( | |
xml.getBytes(StandardCharsets.UTF_8.name()) | |
) | |
); | |
return folder.extractTree() | |
.map(Folder::getName) | |
.filter(s -> s.startsWith(String.valueOf(startingLetter))) | |
.collect(Collectors.toList()); | |
} catch (JAXBException | UnsupportedEncodingException e) { | |
throw new RuntimeException(e.getMessage()); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
String xml = | |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
"<folder name=\"c\">" + | |
"<folder name=\"program files\">" + | |
"<folder name=\"uninstall information\"><folder name=\"unified folder\" /></folder>" + | |
"</folder>" + | |
"<folder name=\"users\" />" + | |
"</folder>"; | |
Collection<String> names = folderNames(xml, 'u'); | |
names.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment