试图在XML字符串Java中获取标签的值

发布于 2025-01-29 10:52:27 字数 1036 浏览 3 评论 0原文

我有一个存储在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();

但是我不知道该怎么办,并且非常迷失 感谢您的帮助

I have an xml string stored in a StringBuilder.
My xml looks like this

couldn't write it in code so here's a screenshot

inside the report tag, it looks like
what it looks like

I would like to get access to any tag value I want in the record tag, what I have is :

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();

But I do not know what to do with this and am very lost
Thanks by advance for helping

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2025-02-05 10:52:27

通常,您可以使用以下例程,

Element documentElement=....
NodeList elmList=documentElement.getElementsByTagName("elementName");
Element e=(Element)elmList.itm(x);//putting it in a loop would do

可以继续使用以上来递归获取元素。

虽然一种更好的方法是使用XPath( saxon Elastimaton,尽管还有更多的库可供选择)

In general, you can use the below routine

Element documentElement=....
NodeList elmList=documentElement.getElementsByTagName("elementName");
Element e=(Element)elmList.itm(x);//putting it in a loop would do

You could keep using the above to get elements recursively.

Though a better approach would be to use XPath (Saxon has a decent XPath implementaton, though there are many more libraries to choose from)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文