以下是我的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>
我需要知道如何将
标记中的所有值追加到ArrayList
中。
如下所示:
Agent[0] = UtilityCompany
Agent[1] = LargeCustomer
electricityPlan[0] = PlanUtility
electricityPlan[1] = LargecPlan
role[0] = Seller
role[1] = Buyer
公共静态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;
}
}
与其为每个类型使用arraylist
,不如考虑采用面向对象(Object Oriented)方法。您将注意到这种方法的许多优点,比如(大量)更少的变量和代码行。例如:
driver.java:
public class Driver {
public static final String XML_FILE_PATH = "zin/zin.xml";
/**
* Entry point of the program.
*/
public static void main(String[] args) throws IOException, JDOMException {
File xmlFile = new File(XML_FILE_PATH);
// the SAXBuilder is the easiest way to create the JDOM2 objects
SAXBuilder jdomBuilder = new SAXBuilder();
// jdomDocument is the JDOM2 Object
Document jdomDocument = jdomBuilder.build(xmlFile);
Element root = jdomDocument.getRootElement();
Element negotiation = root.getChild("negotiation");
// get the list of children agent elements
List<Element> xmlAgents = negotiation.getChildren("agent");
// list for holding the agent objects parsed from the XML file
List<Agent> agents = new ArrayList<Agent>();
// iterate over the agent nodes from the XML file and create
// agent objects to represent them
for (Element currentAgent : xmlAgents) {
Agent newAgent = new Agent();
// set the new agent's 'AgentType' before descending into
// the children XML nodes
newAgent.setAgentType(currentAgent.getChildText("type"));
// recursively finish the new agent
descend(currentAgent, newAgent);
// add the new agent to the ArrayList of agents
agents.add(newAgent);
}
// print agent data to output
for(Agent agent : agents) {
System.out.println(agent);
}
}
/**
* Note: Agent is not immutable, this method alters the members
* of the passed in Agent object.
*
* Recursive method to fill in the rest of a passed in Agent object.
*
* @param ele - an XML element to descend.
* @param agent - the Agent object to finish filling.
*/
public static void descend(Element ele, Agent agent) {
for (Element e : ele.getChildren()) {
switch (e.getName()) {
case "electricityPlan":
agent.setPlanType(e.getChildText("type"));
break;
case "role":
agent.setRoleType(e.getChildText("type"));
break;
case "electricityOrder":
agent.setOrderType(e.getChildText("type"));
break;
default:
break;
}
descend(e, agent);
}
}
}
agent.java
public class Agent {
private String auctionType;
private String agentType;
private String planType;
private String roleType;
private String orderType;
public Agent() {
auctionType = "";
agentType = "";
planType = "";
roleType = "";
orderType = "";
}
@Override
public String toString() {
String str = "Agent\r\n";
str += " AuctionType: " + getAuctionType() + "\r\n";
str += " AgentType: " + getAgentType() + "\r\n";
str += " RoleType: " + getRoleType() + "\r\n";
str += " PlanType: " + getPlanType() + "\r\n";
str += " OrderType: " + getOrderType();
return str;
}
public String getAuctionType() {
return auctionType;
}
public void setAuctionType(String auctionType) {
this.auctionType = auctionType;
}
public String getAgentType() {
return agentType;
}
public void setAgentType(String agentType) {
this.agentType = agentType;
}
public String getPlanType() {
return planType;
}
public void setPlanType(String planType) {
this.planType = planType;
}
public String getRoleType() {
return roleType;
}
public void setRoleType(String roleType) {
this.roleType = roleType;
}
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
}
Agent
AuctionType:
AgentType: UtilityCompany
RoleType: Seller
PlanType: PlanUtility
OrderType: SalesOrder
Agent
AuctionType:
AgentType: LargeCustomer
RoleType: Buyer
PlanType: LargecPlan
OrderType: PurchaseOrder
下面,我有一个全局应用的日志回溯配置。 我想全局使日志记录级别错误,除了一个类。对于一个特定的类,我想使日志记录级别 INFO。我该怎么做?
我使用codeigniter和有一个表3列(id,名称,parent_id)。一个类别可以有多个子类别,一个子类别可以有多个子类别。 我一直在尝试使用以下代码获取所有类别及其子类别: 但它只返回1级类别。 我的问题是如何完成我的任务:获得每个级别的所有类别和子类别。
下面是XML 我做错了什么..
在上面的XML中,父节点和它的一个子节点(即中心)有相同的标记名。我知道我可以在主标记的索引0处进行多个步骤的解析,然后中心标记的索引0将给出ID,中心标记的索引1将给出中心的值333,等等,但是有没有一种方法可以直接得到ID和中心的值(11,333)。
问题内容: 我的logback.xml中有这个添加器 因此,目前我将所有日志保存到一个文件中。如何使它像一个文件仅保存错误日志,而另一个文件保存所有其他日志? 我想在代码中仅使用1个logger实例,如下所示: 问题答案: 开始寻找logback类别,找到过滤器。 只需将过滤器说明添加到您的附加程序即可:
我有点小问题,找不到解决办法。我想为级别信息而不是级别警告设置图案布局。如果我有一个级别信息的日志,一切都正常,但是如果日志是级别,它会被写入控制台两次(作为级别信息和作为级别警告)。只需将特定级别的所有日志写入该级别和以下级别的us日志即可。 我想将级别信息写入控制台,如:和级别如。