3 回答
TA贡献1829条经验 获得超6个赞
在不使用 XPath 的情况下,您可以:
NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");
System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());
TA贡献2021条经验 获得超8个赞
使用他的解决方案,我尝试了它的工作
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XpathTester {
public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
File inputFile = new File("C:\\Users\\Arvind.Carpenter\\Desktop\\input.txt");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputFile);
doc.getDocumentElement().normalize();
NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");
//you can iterate over this node list and get all the URL i am printing first one
System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());
}
}
TA贡献1775条经验 获得超11个赞
以下行有问题:-
Document doc = builder.parse(new InputSource(new StringReader(result)));
我尝试打印文档,它给我的输出为 [#document: null],你可以先尝试在控制台中打印文档并让我知道它是否不为空。
添加回答
举报