[英]Trying to get the value of a tag in an xml string java
我有一個存儲在 StringBuilder 中的 xml 字符串。 我的 xml 看起來像這樣
在報告標簽內,它看起來像它的樣子
我想在記錄標簽中訪問我想要的任何標簽值,我擁有的是:
StringBuilder informationString = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
informationString.append(scanner.nextLine());
}
//Close the scanner
scanner.close();
System.out.println(informationString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(String.valueOf(informationString))));
Element rootElement = document.getDocumentElement();
但我不知道該怎么辦,我很迷茫提前感謝您的幫助
一般來說,您可以使用以下例程
Element documentElement=....
NodeList elmList=documentElement.getElementsByTagName("elementName");
Element e=(Element)elmList.itm(x);//putting it in a loop would do
您可以繼續使用上述方法以遞歸方式獲取元素。
雖然更好的方法是使用 XPath( Saxon有一個不錯的 XPath 實現,但還有更多庫可供選擇)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.