【问题标题】:Reading contents of the XML using java使用 java 读取 XML 的内容
【发布时间】:2011-08-21 04:57:45
【问题描述】:

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

由于列标签不是唯一的,我不知道如何阅读它们。谁能帮帮我。

提前致谢。

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

【问题讨论】:

    标签: java xml parsing uri


    【解决方案1】:

    这段代码:

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

    返回一个列节点列表,为什么不直接使用 for 循环遍历它们而不是只读取第一个?

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

    【讨论】:

    • 我根据您的建议更改了代码,但现在打印了一个空点。 System.out.prinln 行打印空点。请检查上面的更新代码。请
    • 查看extraneon的其他回复!
    【解决方案2】:

    您现在获得了 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");
     }
    

    当您在节点中处理文本内容时,请查看the answer to this SO question。文本节点很烦人:

    <foo>
       a
       b
       c
     </foo>
    

    可以是或者是多个foo的子节点,getTextContent()可以缓解一点痛苦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多