import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
// other imports...
public static void printPomDependencies() throws IOException, SAXException, ParserConfigurationException {
// pom relative to your project directory
final File pomFile = new File("./pom.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(pomFile);
doc.getDocumentElement().normalize();
final NodeList dependencyNodes = doc.getElementsByTagName("dependency");
for (int i = 0; i < dependencyNodes.getLength(); i++) {
final Node n = dependencyNodes.item(i);
final NodeList list = n.getChildNodes();
System.out.println("----------------------------------");
for (int j = 0; j < list.getLength(); j++) {
final Node n2 = list.item(j);
// do not print empty text nodes or others...
if (n2.getNodeType() != Node.ELEMENT_NODE) continue;
System.out.println(n2.getNodeName() + ":" + n2.getTextContent());
}
}
}
类示例 { File pomfile = new File(“./pom.xml”); int getchar = new int getchar(); Model model =reader.read(new FileReader(pomfile)); String getString(); for(Dependency dependency:依赖关系) {
2条答案
按热度按时间y1aodyip1#
您可以编写自己的
parser
,这样就可以定义自定义格式。一个非常基本的(只使用jdk内置库)可能看起来像这样:
例如,这个解析器打印以下输出:
9ceoxa922#
类示例
{
File pomfile = new File(“./pom.xml”);
int getchar = new int getchar();
Model model =reader.read(new FileReader(pomfile));
String getString();
for(Dependency dependency:依赖关系)
{
}
}
所需依赖性