【发布时间】:2017-03-07 19:16:25
【问题描述】:
以下是我的 XML 文件:
<scenario>
<negotiation>
<type>auction</type>
<agent>
<type>UtilityCompany</type>
<electricityPlan>
<type>PlanUtility</type>
<offeredEnergy>10</offeredEnergy>
<duration>20</duration>
<price>10</price>
<role>
<type>Seller</type>
</role>
<electricityOrder>
<type>SalesOrder</type>
</electricityOrder>
</electricityPlan>
</agent>
<agent>
<type>LargeCustomer</type>
<electricityPlan>
<type>LargecPlan</type>
<requestedEnergy>100</requestedEnergy>
<duration>20</duration>
<price>10</price>
<role>
<type>Buyer</type>
</role>
<electricityOrder>
<type>PurchaseOrder</type>
</electricityOrder>
</electricityPlan>
</agent>
</negotiation>
</scenario>
我需要知道如何将<type></type> 标签内的所有值附加到ArrayList。
类似于以下内容:
Agent[0] = UtilityCompany
Agent[1] = LargeCustomer
electricityPlan[0] = PlanUtility
electricityPlan[1] = LargecPlan
role[0] = Seller
role[1] = Buyer
有人可以提供一些见解吗?
更新信息
目前,我有这个:
public static void main(String[] args) 抛出 ParserConfigurationException、SAXException、IOException、XPathExpressionException {
File fXmlFile = new File("scenario.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nodes = doc.getElementsByTagName("negotiation");
Element element = (Element) nodes.item(0);
NodeList negotiationList = element.getElementsByTagName("agent");
for (int i = 0; i < negotiationList.getLength(); i++) {
Element negotiationElement = (Element) negotiationList.item(i);
NodeList agent = negotiationElement.getElementsByTagName("type");
System.out.println(agent.item(0).getFirstChild().getTextContent());
NodeList role = negotiationElement.getElementsByTagName("role");
Element roleline = (Element) role.item(0);
System.out.println(roleline.getElementsByTagName("type").item(0).getTextContent());
}
}
Output
UtilityCompany
Seller
LargeCustomer
Buyer
LargeCustomer
Buyer
大客户 买家
但我不知道如何从
中获取值 1. <type>auction</type>
2. <type>PlanUtility</type> from ElectricityPlan tag.
最后我找到了解决方案:
此代码用于获取请求的节点
我有一个解决方案:
public class ReadingXML {
static List<String> AuctionType = new ArrayList<String>();
static List<String> AgentType = new ArrayList<String>();
static List<String> PlanType = new ArrayList<String>();
static List<String> RoleType = new ArrayList<String>();
static List<String> OrderType = new ArrayList<String>();
static NodeList auction ;
public List<List<String>> ReadingXML(String fileLocation) throws
ParserConfigurationException, SAXException, IOException {
//File fXmlFile = new File("C:....location\\scenario.xml");
File fXmlFile = new File(fileLocation);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
List<List<String>> listOfLists = Lists.newArrayList();
NodeList nodesScenario = doc.getElementsByTagName("scenario");
Element element0 = (Element) nodesScenario.item(0);
NodeList scenarioList = element0.getElementsByTagName("negotiation");
for (int j = 0; j < scenarioList.getLength(); j++)
{
Element scenarioElement = (Element) scenarioList.item(j);
auction = scenarioElement.getElementsByTagName("type");
System.out.println(auction.item(0).getFirstChild().getTextContent());
AuctionType.add(auction.item(0).getFirstChild().getTextContent());
}
List<String> AuctionTypeFiltered = Lists.newArrayList(Sets.newHashSet(AuctionType));
listOfLists.add(Lists.newArrayList(AuctionTypeFiltered));
NodeList nodesNegotiation = doc.getElementsByTagName("negotiation");
Element element = (Element) nodesNegotiation.item(0);
NodeList negotiationList = element.getElementsByTagName("agent");
for (int i = 0; i < negotiationList.getLength(); i++)
{
Element negotiationElement = (Element) negotiationList.item(i);
NodeList agent = negotiationElement.getElementsByTagName("type");
System.out.println(agent.item(0).getFirstChild().getTextContent());
AgentType.add(agent.item(0).getFirstChild().getTextContent());
NodeList nodesElectricityPlan = doc.getElementsByTagName("electricityPlan");
Element element1 = (Element) nodesElectricityPlan.item(0);
NodeList ElectricityPlanList = element1.getElementsByTagName("plan");
for (int j = 0; j < ElectricityPlanList.getLength(); j++)
{
Element electricityPlanElement = (Element) ElectricityPlanList.item(j);
NodeList plan = electricityPlanElement.getElementsByTagName("type");
System.out.println(plan.item(0).getFirstChild().getTextContent());
PlanType.add(plan.item(0).getFirstChild().getTextContent());
}
List<String> PlanTypeFiltered = Lists.newArrayList(Sets.newHashSet(PlanType));
listOfLists.add(Lists.newArrayList(PlanTypeFiltered));
NodeList role = negotiationElement.getElementsByTagName("role");
Element roleline = (Element) role.item(0);
System.out.println(roleline.getElementsByTagName("type").item(0).getTextContent());
RoleType.add(roleline.getElementsByTagName("type").item(0).getTextContent());
NodeList electricityorder = negotiationElement.getElementsByTagName("electricityOrder");
Element electricityorderline = (Element) electricityorder.item(0);
System.out.println(electricityorderline.getElementsByTagName("type").item(0).getTextContent());
OrderType.add(electricityorderline.getElementsByTagName("type").item(0).getTextContent());
}
List<String> AgentTypeFiltered = Lists.newArrayList(Sets.newHashSet(AgentType));
listOfLists.add(Lists.newArrayList(AgentTypeFiltered));
List<String> RoleTypeFiltered = Lists.newArrayList(Sets.newHashSet(RoleType));
listOfLists.add(Lists.newArrayList(RoleTypeFiltered));
List<String> OrderTypeFiltered = Lists.newArrayList(Sets.newHashSet(OrderType));
listOfLists.add(Lists.newArrayList(OrderTypeFiltered));
return listOfLists;
}
}
【问题讨论】:
-
您可能需要使用一些 xml 解析器库。Dom Parser、SAX 解析器、Stax 解析器是其中的一些。
-
是的,我知道,问题是如何获取元素,首先,我正在解析 xml 文件: .... dom = db.parse("file.xml")... .;后来我在解析文档,但我使用的是:NodeList nl = docEle.getElementsByTagName("Agent");
-
你用什么来解析 XML?我会用你正在使用的任何东西写一个答案。
-
我不明白这有什么难的。为什么你不能遍历 dom 并在你下降得更深时记住父元素?当您找到一个
时,您将保存的父级添加到 arrayList...? -
@Calicoder 对我来说困难的部分是获得
auction 和PlanUtility 。谢谢