如何使用 jdom 来检查文件中是否存在某个元素
我有一个像这样的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RootElement>
<Achild>
.....
</Achild>
</RootElement>
How can I check if the File contains Achild
element or not..?
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(configFile);
final Node parentNode = doc.getDocumentElement();
final Element childElement = (Element) parentNode.getFirstChild();
if(childElement.getNodeName().equalsIgnoreCase(....
但它给我的错误是 childElement is null....
I have an XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<RootElement>
<Achild>
.....
</Achild>
</RootElement>
How can I check if the File contains Achild
element or not..?
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(configFile);
final Node parentNode = doc.getDocumentElement();
final Element childElement = (Element) parentNode.getFirstChild();
if(childElement.getNodeName().equalsIgnoreCase(....
buts its giving me error on childElement is null....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的代码中,您只是创建一个构建器,但它会构建什么?你不'指定用于uold DOM树的文件...下面是一个使用JDom的小例子..但是进行搜索和更多XPATH和XQuery是非常惊人的..
In your code u are just creating a builder, but what does it build ?? u dont' specifiy the file used to uold the DOM Tree... Below a small example using JDom.. But to make searches and much more XPATH and XQuery are pretty amazing ..
现在检查它的大小是否包含任何孩子。
now check the size wheather it contains any child or not.