XML测试类
package com.huawei.bss.xml;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class InnerPortXMLParse
{
public static List<InnerXMLBean> parseXML()
{
//xml文件的路径
String xmlFilePath="src/conf/portSlotConfig.xml";
File xmlFile=new File(xmlFilePath);
XmlTree xmlTree=new XmlTree();
try
{
xmlTree =Xml.parseXml(xmlFile);
}
catch (Exception e)
{
e.printStackTrace();
}
return getInnerXMLBeanList(xmlTree.getRoot().getChildrenByTagName("connection"));
}
/**
* 根据跟节点得到一个List
* @param xmlNodeList
* @return
* @see [类、类#方法、类#成员]
*/
private static List<InnerXMLBean> getInnerXMLBeanList(List<XmlNode> xmlNodeList)
{
List<InnerXMLBean> innerXMLBeanList=new ArrayList<InnerXMLBean>(xmlNodeList.size());
InnerXMLBean innerXMLBean;
for (XmlNode xmlNode : xmlNodeList)
{
innerXMLBean =new InnerXMLBean();
String bomValue=getSimpleProperty(xmlNode,"bom");
innerXMLBean.setBomValue(bomValue);
innerXMLBean.setPortToSolts(getPortToSoltBeanList(xmlNode.getChildrenByTagName("portToSlots")));
innerXMLBeanList.add(innerXMLBean);
}
return innerXMLBeanList;
}
/**
* 根据xmlNode获取到PortToSoltBean的数组
* @param portBeanNodeList
* @return
* @see [类、类#方法、类#成员]
*/
private static List<PortToSoltBean> getPortToSoltBeanList(List<XmlNode> portBeanNodeList)
{
if (portBeanNodeList == null || portBeanNodeList.isEmpty())
{
return null;
}
XmlNode portToSolts=portBeanNodeList.get(0);
//得到portToSolt节点的值
List<XmlNode> portToSolt= portToSolts.getChildrenByTagName("portToSlot");
//创建一个List<PortToSoltBean>对象
List<PortToSoltBean> portBeanList=new ArrayList<PortToSoltBean>(portToSolt.size());
PortToSoltBean portBean;
for (XmlNode portBeanNode : portToSolt)
{
portBean=new PortToSoltBean();
portBean.setPort(getSimpleProperty(portBeanNode,"port"));
portBean.setSolt(getSimpleProperty(portBeanNode,"Slot"));
portBean.setInnerType(getSimpleProperty(portBeanNode,"type"));
portBeanList.add(portBean);
}
return portBeanList;
}
/**
* 根据节点名获取XML对应节点的值
* @param parent 节点
* @param parent simplePropertyTagName 节点名称
* @return
*/
private static String getSimpleProperty(XmlNode parent,String simplePropertyTagName)
{
List<XmlNode> simpleNode = parent.getChildrenByTagName(simplePropertyTagName);
if (simpleNode == null || simpleNode.size() != 1)
{
return null;
}
return simpleNode.get(0).getText();
}
public static void main(String[] args)
{
List<InnerXMLBean> listBean=InnerPortXMLParse.parseXML();
for (Iterator iterator = listBean.iterator(); iterator.hasNext();)
{
InnerXMLBean innerXMLBean = (InnerXMLBean) iterator.next();
System.out.println(innerXMLBean.getBomValue()+"==="+innerXMLBean.getIndexNumber());
}
}
}