使用java读取XML内容

发布于 2024-11-05 14:50:16 字数 2143 浏览 3 评论 0原文

我正在尝试读取 XML 文件 使用java。我可以成功读取文件,但问题是,我不知道如何读取列标记内的值。

由于列标签不是唯一的,我不知道如何阅读它们。有人可以帮助我吗?

提前致谢。

 import java.net.URL;
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.NodeList;

public class XMLReader {

 public static void main(String argv[]) {

  try {
      //new code
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new URL("http://www.cse.lk/listedcompanies/overview.htm?d-16544-e=3&6578706f7274=1").openStream());

      doc.getDocumentElement().normalize();
      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
      NodeList nodeLst = doc.getElementsByTagName("row");
      System.out.println("Information of all Stocks");

      for (int s = 0; s < nodeLst.getLength(); s++) {

        Node fstNode = nodeLst.item(s);

        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

          Element fstElmnt = (Element) fstNode;
          //NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");
          //Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          //NodeList fstNm = fstNmElmnt.getChildNodes();
          //System.out.println("First Tag : "  + ((Node) fstNm.item(0)).getNodeValue());
          NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("column");
         // Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

          for (int columnIndex = 0; columnIndex < lstNmElmntLst.getLength(); columnIndex++) {
              Element lstNmElmnt = (Element) lstNmElmntLst.item(columnIndex);
              NodeList lstNm = lstNmElmnt.getChildNodes();
              System.out.println("Last Tag : " + ((Node) lstNm.item(0)).getNodeValue());
              }

        }

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

I'm trying to read an XML file using java. I can sucessfully read the file but the problem is, I don't know how to read the values inside the column tag.

Since the column tags are not unique, I have no idea how to read them. Can someone help me.

Thanks in advance.

 import java.net.URL;
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.NodeList;

public class XMLReader {

 public static void main(String argv[]) {

  try {
      //new code
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new URL("http://www.cse.lk/listedcompanies/overview.htm?d-16544-e=3&6578706f7274=1").openStream());

      doc.getDocumentElement().normalize();
      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
      NodeList nodeLst = doc.getElementsByTagName("row");
      System.out.println("Information of all Stocks");

      for (int s = 0; s < nodeLst.getLength(); s++) {

        Node fstNode = nodeLst.item(s);

        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

          Element fstElmnt = (Element) fstNode;
          //NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");
          //Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          //NodeList fstNm = fstNmElmnt.getChildNodes();
          //System.out.println("First Tag : "  + ((Node) fstNm.item(0)).getNodeValue());
          NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("column");
         // Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

          for (int columnIndex = 0; columnIndex < lstNmElmntLst.getLength(); columnIndex++) {
              Element lstNmElmnt = (Element) lstNmElmntLst.item(columnIndex);
              NodeList lstNm = lstNmElmnt.getChildNodes();
              System.out.println("Last Tag : " + ((Node) lstNm.item(0)).getNodeValue());
              }

        }

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

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

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

发布评论

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

评论(2

耳钉梦 2024-11-12 14:50:16

此代码:

NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");

返回列节点的列表,为什么不只使用 for 循环来迭代所有列节点,而不是只读取第一个节点?

for (int columnIndex = 0; columnIndex < fstNmElmntLst.getLength(); columnIndex++) {
Element fstNmElmnt = (Element) fstNmElmntLst.item(columnIndex);
...
}

This code :

NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");

Return a List of column nodes, why not just use a for loop to iterate over them all instead of just reading the first one ?

for (int columnIndex = 0; columnIndex < fstNmElmntLst.getLength(); columnIndex++) {
Element fstNmElmnt = (Element) fstNmElmntLst.item(columnIndex);
...
}
送你一个梦 2024-11-12 14:50:16

您现在得到一个 NPE:

<column/>

并且您应该在获取元素 0 之前检查列表大小:

 NodeList lstNm = lstNmElmnt.getChildNodes();
 if (lstNm.getLength() > 0) {
    System.out.println("Last Tag : " + ((Node)lstNm.item(0)).getNodeValue());
 } else {
     System.out.println("No content");
 }

当您处理节点中的文本内容时,请查看 这个问题的答案。文本节点令人恼火,因为:

<foo>
   a
   b
   c
 </foo>

可以是或不止一个 foo 的子节点,而 getTextContent() 可以稍微缓解这一痛苦。

You now get a NPE on:

<column/>

and you should check your list size before getting element 0:

 NodeList lstNm = lstNmElmnt.getChildNodes();
 if (lstNm.getLength() > 0) {
    System.out.println("Last Tag : " + ((Node)lstNm.item(0)).getNodeValue());
 } else {
     System.out.println("No content");
 }

And as you're processing text content in nodes, have a look at the answer to this SO question. Text nodes are irriting as:

<foo>
   a
   b
   c
 </foo>

can be or are more than one child node of foo, and getTextContent() can ease the pain a bit.

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