使用java,我如何从网站读取xml文件,然后将xml文件中的数据放入JList中?

发布于 2024-12-01 16:09:01 字数 543 浏览 3 评论 0原文

这是xml文件的想法:

<date>
<aug30>
    <item1>This is an item for August 30</item1>
    <item2>This is another item for August 30</item2>
</aug30>
<aug31>
    <item1>This is an item for August 31</item1>
    <item2>This is another item for August 31</item2>
    <item3>This is a 3rd item for August 31</item3>
</aug31>
</date>

我试图弄清楚如何做,例如,在8月30日,将aug30标签中的项目1和2放入JList中,并在8月31日,将项目1, 2 和 3 从 aug31 标签到同一个 JList 中。

Here's the idea of the xml file:

<date>
<aug30>
    <item1>This is an item for August 30</item1>
    <item2>This is another item for August 30</item2>
</aug30>
<aug31>
    <item1>This is an item for August 31</item1>
    <item2>This is another item for August 31</item2>
    <item3>This is a 3rd item for August 31</item3>
</aug31>
</date>

What I'm trying to figure out how to do is, for example, on August 30, put items 1 and 2 in the aug30 tags into a JList, and on August 31, put items 1, 2 and 3 from the aug31 tags into that same JList.

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

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

发布评论

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

评论(1

优雅的叶子 2024-12-08 16:09:01

干得好。这仅查找基于日期的部分。我没有展示添加到 JList,因为这很简单。只需更改打印 ADD 的打印语句即可。

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    "<date>\n" +
    "<aug30>\n" +
    "    <item1>This is an item for August 30</item1>\n" +
    "    <item2>This is another item for August 30</item2>\n" +
    "</aug30>\n" +
    "<aug31>\n" +
    "    <item1>This is an item for August 31</item1>\n" +
    "    <item2>This is another item for August 31</item2>\n" +
    "    <item3>This is a 3rd item for August 31</item3>\n" +
    "</aug31>\n" +
    "</date>\n";

    SimpleDateFormat df = new SimpleDateFormat("MMMd");
    String date = df.format(new Date()).toLowerCase();

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));

        DocumentTraversal traversal = (DocumentTraversal) doc;

        NodeIterator iterator = traversal.createNodeIterator(
          doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

        for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
            String tagname = ((Element) n).getTagName();
            //System.out.println("Compare TAG: '" + tagname + "' with '" + date + "'");
            if(tagname.equals(date)) {
                n = iterator.nextNode();
                tagname = ((Element) n).getTagName();
                while(tagname.startsWith("item") && n != null) {
                    System.out.println("ADD: " + ((Element)n).getTextContent());
                    n = iterator.nextNode();
                    if(n != null) {
                        tagname = ((Element) n).getTagName();
                    } 
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

进口产品为:

import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;

Here you go. This finds only the sections based on the date. I haven't shown the adding to JList, as thats easy. Just change the print statement which prints ADD and you are good to go.

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    "<date>\n" +
    "<aug30>\n" +
    "    <item1>This is an item for August 30</item1>\n" +
    "    <item2>This is another item for August 30</item2>\n" +
    "</aug30>\n" +
    "<aug31>\n" +
    "    <item1>This is an item for August 31</item1>\n" +
    "    <item2>This is another item for August 31</item2>\n" +
    "    <item3>This is a 3rd item for August 31</item3>\n" +
    "</aug31>\n" +
    "</date>\n";

    SimpleDateFormat df = new SimpleDateFormat("MMMd");
    String date = df.format(new Date()).toLowerCase();

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));

        DocumentTraversal traversal = (DocumentTraversal) doc;

        NodeIterator iterator = traversal.createNodeIterator(
          doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

        for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
            String tagname = ((Element) n).getTagName();
            //System.out.println("Compare TAG: '" + tagname + "' with '" + date + "'");
            if(tagname.equals(date)) {
                n = iterator.nextNode();
                tagname = ((Element) n).getTagName();
                while(tagname.startsWith("item") && n != null) {
                    System.out.println("ADD: " + ((Element)n).getTextContent());
                    n = iterator.nextNode();
                    if(n != null) {
                        tagname = ((Element) n).getTagName();
                    } 
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

The imports are:

import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文