3个回答
展开全部
给你个例子让你参考下~~
Java类:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 XmlReader
{
public static void readXml(String inputXmlPath, List<ServiceInfo> infos)
throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory docfac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docfac.newDocumentBuilder();
InputStream in = new FileInputStream(inputXmlPath);
Document doc = builder.parse(in);
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName("service");
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
NodeList childNodes = node.getChildNodes();
ServiceInfo info = new ServiceInfo();// 位置上移
for (int j = 0; j < childNodes.getLength(); j++)
{
Node child = childNodes.item(j);
if (Node.ELEMENT_NODE == child.getNodeType())
{
if ("name".equals(child.getNodeName()))
{
info.setName(child.getTextContent());
}
if ("class".equals(child.getNodeName()))
{
info.setClassName(child.getTextContent());
}
}
}
infos.add(info);// 位置上移出现什么结果
}
}
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException
{
List<ServiceInfo> infos = new ArrayList<ServiceInfo>();
readXml("F:\\services.xml", infos);
System.out.println(infos);
}
}
class ServiceInfo
{
private String name;
private String className;
public String getClassName()
{
return className;
}
public void setClassName(String className)
{
this.className = className;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return new StringBuffer().append("name=" + name).append(
"class=" + className).toString();
}
}
XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<services>
<service>
<name>liaoliang</name>
<class>com.chinasoft.Liao</class>
</service>
<service>
<name>erji</name>
<class>com.chinasoft.Erji</class>
</service>
</services>
Java类:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 XmlReader
{
public static void readXml(String inputXmlPath, List<ServiceInfo> infos)
throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory docfac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docfac.newDocumentBuilder();
InputStream in = new FileInputStream(inputXmlPath);
Document doc = builder.parse(in);
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName("service");
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
NodeList childNodes = node.getChildNodes();
ServiceInfo info = new ServiceInfo();// 位置上移
for (int j = 0; j < childNodes.getLength(); j++)
{
Node child = childNodes.item(j);
if (Node.ELEMENT_NODE == child.getNodeType())
{
if ("name".equals(child.getNodeName()))
{
info.setName(child.getTextContent());
}
if ("class".equals(child.getNodeName()))
{
info.setClassName(child.getTextContent());
}
}
}
infos.add(info);// 位置上移出现什么结果
}
}
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException
{
List<ServiceInfo> infos = new ArrayList<ServiceInfo>();
readXml("F:\\services.xml", infos);
System.out.println(infos);
}
}
class ServiceInfo
{
private String name;
private String className;
public String getClassName()
{
return className;
}
public void setClassName(String className)
{
this.className = className;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return new StringBuffer().append("name=" + name).append(
"class=" + className).toString();
}
}
XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<services>
<service>
<name>liaoliang</name>
<class>com.chinasoft.Liao</class>
</service>
<service>
<name>erji</name>
<class>com.chinasoft.Erji</class>
</service>
</services>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你去找一个DOM解析XML的例子,研究下就好了,很简单的。
更多追问追答
追问
做这个需要什么特殊工具吗?
追答
做这个不需要什么特殊的工具,就用下eclipse就可以了,很简单的。
由于解析XML,已经有接口可是帮你实现了,就是DOM
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同楼上
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询