gpt4 book ai didi

JAVA - 解析 XML 文件未正确存储数据

转载 作者:行者123 更新时间:2023-12-01 19:11:42 26 4
gpt4 key购买 nike

我正在很好地解析 XML 文件,除了它不保存找到的第一个数据而只保存第二个(最后一个)

这是文件:

package main;

import java.io.File;
import java.util.ArrayList;

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;

import main.advertise.Advert;
import main.advertise.guilds.Guild;
import main.advertise.server.Server;

public class Servers {

//private Server[] servers;
//private Guild[] guilds;

private ArrayList<Server> servers = new ArrayList<Server>();
private ArrayList<Guild> guilds = new ArrayList<Guild>();



@SuppressWarnings("null")
public void init() {

Server server = new Server("", "", "", "", new String[] {}, new String[] {}, 0, 0, 0);
Guild guild = new Guild("", "", "", "", new String[] {}, 0);

try {




File fXmlFile = new File("C:/Users/NULL/Desktop/Adverts.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();



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

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("Server Name - " + eElement.getElementsByTagName("servername").item(0).getTextContent());
server.setServerName(eElement.getElementsByTagName("servername").item(0).getTextContent());
server.setDiscordLink(eElement.getElementsByTagName("discordlink").item(0).getTextContent());
server.setOwnerId(eElement.getElementsByTagName("ownerid").item(0).getTextContent());
server.setLogoUrl(eElement.getElementsByTagName("logourl").item(0).getTextContent());



NodeList features = doc.getElementsByTagName("feature");

String[] featureList = new String[features.getLength()];

for (int i = 0; i < features.getLength(); i++) {
Node node = features.item(i);
featureList[i]=features.item(i).getTextContent();
System.out.println(features.item(i).getTextContent() + " - new");
}
server.setServerFeatures(featureList);




NodeList images = doc.getElementsByTagName("image");

String[] namesArray = new String[images.getLength()];

for (int i = 0; i < images.getLength(); i++) {
Node node = images.item(i);
namesArray[i]=images.item(i).getTextContent();
System.out.println(images.item(i).getTextContent() + " - new");
}
server.setServerImages(namesArray);






guild.setGuildId(eElement.getElementsByTagName("id").item(0).getTextContent());
guild.setGuildName(eElement.getElementsByTagName("guildname").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("guildname").item(0).getTextContent());
guild.setAdvertiseChannel(eElement.getElementsByTagName("advertchannel").item(0).getTextContent());
guild.setCommandChannel(eElement.getElementsByTagName("commandchannel").item(0).getTextContent());

NodeList users = doc.getElementsByTagName("user");

String[] namessArray = new String[users.getLength()];

for (int i = 0; i < users.getLength(); i++) {
Node node = users.item(i);
namessArray[i]=users.item(i).getTextContent();
System.out.println(users.item(i).getTextContent() + " - new users");
}
guild.setAuthorisedUsers(namessArray);


guild.setAdvertisementDelay(Integer.parseInt(eElement.getElementsByTagName("delay").item(0).getTextContent()));
//guilds[tempp] = guild;
guilds.add(guild);
servers.add(server);
Advert advert = new Advert(guild, server);
Main.servers.add(advert);

}

}



} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Size: ----------------- " + servers.size());
for(int i = 0; i < servers.size(); i++) {
if(guilds.get(i) == null)
return;
Advert v = new Advert(guilds.get(i), servers.get(i), servers.get(i).getServerName());
if(!Main.servers.contains(v)) {
//Main.servers.add(new Advert(guilds.get(i), servers.get(i), servers.get(i).getServerName()));
}
}
System.out.println(" "+Main.servers.get(0).getServer().getServerName());
System.out.println(" "+Main.servers.get(0).getGuild().getGuildName());
System.out.println(" "+Main.servers.get(1).getServer().getServerName());
System.out.println(" "+Main.servers.get(1).getGuild().getGuildName());

}

}

这是 XML 文件:

<advertisements>

<advertisement>
<server>
<servername>Zenith</servername>
<discordlink>http://zenithps.org</discordlink>
<ownerid>304558905306906624</ownerid>
<logourl>http://zenithps.org/Zenith%20Logo.png</logourl>
<feature>Feature1</feature>
<feature>Feature2</feature>
<image>C:/Users/NULL/Pictures/Zenith Logo.png</image>
<image>C:/Users/NULL/Pictures/Description Hovers.gif</image>
<image>C:/Users/NULL/Pictures/Launcher.png</image>
<image>C:/Users/NULL/Pictures/Upgrade.gif</image>
<image>C:/Users/NULL/Pictures/Pet.png</image>
</server>
<guild>
<id>642406906580828181</id>
<guildname>Guild</guildname>
<advertchannel>658936539803418624</advertchannel>
<commandchannel>658936539803418624</commandchannel>
<user>304558905306906624</user>
<delay>25000</delay>
</guild>
</advertisement>


<advertisement>
<server>
<servername>Test Server</servername>
<discordlink>http://zenithps.org</discordlink>
<ownerid>304558905306906624</ownerid>
<logourl>http://zenithps.org/Zenith%20Logo.png</logourl>
<feature>Feature1</feature>
<feature>Feature2</feature>
<image>C:/Users/NULL/Pictures/Zenith Logo.png</image>
<image>C:/Users/NULL/Pictures/Description Hovers.gif</image>
<image>C:/Users/NULL/Pictures/Launcher.png</image>
<image>C:/Users/NULL/Pictures/Upgrade.gif</image>
<image>C:/Users/NULL/Pictures/Pet.png</image>
</server>
<guild>
<id>6424069110828181</id>
<guildname>Test Guild</guildname>
<advertchannel>658936539803418624</advertchannel>
<commandchannel>658936539803418624</commandchannel>
<user>304558905306906624</user>
<delay>25000</delay>
</guild>
</advertisement>
</advertisements>

打印时

System.out.println("    "+Main.servers.get(0).getServer().getServerName());
System.out.println(" "+Main.servers.get(0).getGuild().getGuildName());
System.out.println(" "+Main.servers.get(1).getServer().getServerName());
System.out.println(" "+Main.servers.get(1).getGuild().getGuildName());

我收到回复了 测试服务器 测试公会 测试服务器 测试公会

但是,这应该是 真力时 公会 测试服务器 测试公会

它在解析文件时使用以下方法打印 Zenith 和 Guild:

System.out.println("Server Name - " + eElement.getElementsByTagName("servername").item(0).getTextContent());

System.out.println(eElement.getElementsByTagName("guildname").item(0).getTextContent());

但它没有正确存储它。我尝试过移动一些东西,但似乎无法做到正确。

最佳答案

这里 Main.servers.get(0) 和 Main.servers.get(1) 都指向同一个实例。对于每个服务器元素,只有一个实例被一次又一次修改,而不是创建一个新的服务器/公会实例,因此服务器/公会将拥有最后一个元素的数据。请将初始化移至 for 循环内,它应该可以正常工作。
请移步

Server server = new Server("", "", "", "", new String[] {}, new String[] {}, 0, 0, 0);
Guild guild = new Guild("", "", "", "", new String[] {}, 0);

内部

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

关于JAVA - 解析 XML 文件未正确存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477169/

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