简体   繁体   English

DOM XML Parser示例

[英]DOM XML Parser Example

I have this XML file.I just parse this XML file.This example shows how to get the node by “name”, and display the value.How to show all records from database? 我有这个XML文件。我只是解析这个XML文件。这个例子展示了如何通过“name”获取节点,并显示值。如何显示数据库中的所有记录?

<data399173_eff_sor>
<record>
    <ID>1</ID>
    <item_no>1.0</item_no>
    <description>Hack off tiles and make good walls</description>
    <price>100</price>
    <base_qty>50</base_qty>
    <var_qty>20</var_qty>
    <base_price_>5000</base_price_>   
</record>
<record>
    <ID>1</ID>
    <item_no>1.03</item_no>
    <description>Test</description>
    <price>45</price>
    <base_qty>100</base_qty>
    <var_qty>4500</var_qty>
    <base_price_>0</base_price_>
</record>
</data399173_eff_sor>

and so on 等等

Java code Java代码

   File fXmlFile = new File("D:/formdata.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("record");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

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

            Element eElement = (Element) nNode;

            System.out.println("Item No : " + eElement.getElementsByTagName("item_no").item(0).getTextContent());
            System.out.println("Description : " + eElement.getElementsByTagName("description").item(0).getTextContent());
            System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
            System.out.println("base qty : " + eElement.getElementsByTagName("base_qty").item(0).getTextContent());
            System.out.println("Var qty : " + eElement.getElementsByTagName("var_qty").item(0).getTextContent());
            System.out.println("Base price : " + eElement.getElementsByTagName("base_price_").item(0).getTextContent());                

        }

In this its just show first record.i want to display all records in the data base 在这个它只显示第一个记录。我想显示数据库中的所有记录

The xml is not valid. xml无效。 (You can validate your xml online: http://www.w3schools.com/xml/xml_validator.asp ) (您可以在线验证您的xml: http//www.w3schools.com/xml/xml_validator.asp

You can try with this xml 您可以尝试使用此xml

<records>
 <record>
  <ID>1</ID>
  <item_no>1.0</item_no>
  <description>Hack off tiles and make good walls</description>
  <price>100</price>
  <base_qty>50</base_qty>
  <var_qty>20</var_qty>
  <base_price_>5000</base_price_>   
 </record>
 <record>
  <ID>1</ID>
  <item_no>1.03</item_no>
  <description>Test</description>
  <price>45</price>
  <base_qty>100</base_qty>
  <var_qty>4500</var_qty>
  <base_price_>0</base_price_>
 </record>
</records>

and keep your code 并保留你的代码

package test;

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

import java.io.File;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class TestXml{ 
 public static void main (String[] args) throws ParserConfigurationException{
     TestXml t = new TestXml();
     t.readXml() ;
   } 
 public void readXml () throws ParserConfigurationException{
    File fXmlFile = new File("D:/formdata.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = null;
    try {
        doc = dBuilder.parse(fXmlFile);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("record");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

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

            Element eElement = (Element) nNode;

            System.out.println("Item No : " + eElement.getElementsByTagName("item_no").item(0).getTextContent());
            System.out.println("Description : " + eElement.getElementsByTagName("description").item(0).getTextContent());
            System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
            System.out.println("base qty : " + eElement.getElementsByTagName("base_qty").item(0).getTextContent());
            System.out.println("Var qty : " + eElement.getElementsByTagName("var_qty").item(0).getTextContent());
            System.out.println("Base price : " + eElement.getElementsByTagName("base_price_").item(0).getTextContent());                

        }}}}

and you will have this result 你会得到这个结果

Root element :records
----------------------------

Current Element :record
Item No : 1.0
Description : Hack off tiles and make good walls
price : 100
base qty : 50
Var qty : 20
Base price : 5000

Current Element :record
Item No : 1.03
Description : Test
price : 45
base qty : 100
Var qty : 4500
Base price : 0

I don't think your XML is valid -- you're only allowed to have one root element in XML. 我不认为你的XML是有效的 - 你只允许在XML中有一个根元素。

So when you do: 所以当你这样做时:

NodeList nList = doc.getElementsByTagName("record");

You're only going to get one element. 你只会得到一个元素。 which is that first <record>...</record> 这是第一个<record>...</record>

In order to fix this, you'll need to wrap all of your <record> tags in some sort of a root element like this: 为了解决这个问题,你需要将所有<record>标签包装在某种根元素中,如下所示:

<root>
  <record>
    <id>1</id>
  </record>
  <record>
    <id>2</id>
  </record>
  ...
</root>

and you'll have to say: 你不得不说:

NodeList nList = doc.getDocumentElement().getElementsByTagName("record");

You can try this code in the loop. 您可以在循环中尝试此代码。

Node nNode = nList.item(temp);
NodeList list = nNode.getChildNodes();
list.item(0).getTextContent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM