gpt4 book ai didi

java - DOM XML 解析器示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:59 26 4
gpt4 key购买 nike

我有这个 XML 文件。我只是解析这个 XML 文件。这个例子展示了如何通过“名称”获取节点,并显示值。如何显示数据库中的所有记录?

<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>

等等

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

}

这里只显示第一条记录。我想显示数据库中的所有记录

最佳答案

xml 无效。 (您可以在线验证您的 xml:http://www.w3schools.com/xml/xml_validator.asp)

你可以试试这个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>

并保留你的代码

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

}}}}

你会得到这样的结果

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

关于java - DOM XML 解析器示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com