簡體   English   中英

如何從 XML java 中的標簽內獲取圖片鏈接

[英]How can I get the link of an image from inside the tag in XML java

我正在嘗試讀取 XML 文件並將標簽之間的值存儲為字符串。 但是對於一個標簽<picture url="https://cloudstor.aa.net.edu.au/plus/s/62e0uExNviPanZE/download"/>我需要獲取圖像的 URL 並存儲它以便我可以使用它在未來。

我用了

File stocks = new File("gui.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(stocks);
        doc.getDocumentElement().normalize();
        System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
        NodeList nodes = doc.getElementsByTagName("billboard");
        System.out.println("==========================");
        Node node = nodes.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
             //message = getValue("message", element);
             information = getValue("information", element).trim();
            String picture = getValue("picture url=", element);
        } 
private static String getValue(String tag, Element element) {
    NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
    Node node = (Node) nodes.item(0);
    return node.getNodeValue();
}

這適用於從普通標簽中獲取信息,例如此處的信息標簽。 知道如何讓它適用於圖片標簽嗎?

 <billboard class="nodarken"> <information> Billboard with an information tag and nothing else. Note that the text is word-wrapped. The quick brown fox jumped over the lazy dogs. </information> <picture url="https://cloudstor.aa.net.edu.au/plus/s/62e0uExNviPanZE/download"/> </billboard>

使用<link><![CDATA[ your url goes here ]]></link>這種格式

 <billboard class="nodarken"> <information> Billboard with an information tag and nothing else. Note that the text is word-wrapped. The quick brown fox jumped over the lazy dogs. </information> <picture><:[CDATA[ https.//cloudstor.aa.net.edu.au/plus/s/62e0uExNviPanZE/download"]]></picture> </billboard>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM