Skip to content

Instantly share code, notes, and snippets.

@Jun711
Created February 16, 2018 00:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jun711/c06663c98d519f5bb400914732ad76bd to your computer and use it in GitHub Desktop.
Save Jun711/c06663c98d519f5bb400914732ad76bd to your computer and use it in GitHub Desktop.
TestDome - Read XML - Folders
import java.util.Collection;
import java.util.LinkedList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import java.io.StringReader;
public class Folders {
public static void searchForLetter(NodeList nodeList, Collection<String> result, char startingLetter) {
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
Element e = (Element) tempNode;
String folderName = e.getAttribute("name");
if (folderName.charAt(0) == startingLetter)
result.add(folderName);
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
if (tempNode.hasChildNodes()) {
searchForLetter(tempNode.getChildNodes(), result, startingLetter);
}
}
}
return;
}
public static Collection<String> folderNames(String xml, char startingLetter) throws Exception {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
Collection<String> result = new LinkedList<String>();
searchForLetter(doc.getChildNodes(), result, startingLetter);
return result;
}
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>" +
"<folder name=\"users\" />" +
"</folder>";
Collection<String> names = folderNames(xml, 'u');
for(String name: names)
System.out.println(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment