Java 递归XML解析器
我有以下xml文件:Java 递归XML解析器,java,xml,recursion,xml-parsing,Java,Xml,Recursion,Xml Parsing,我有以下xml文件: <?xml version="1.0"?> <CONFIG> <FUNCTION> <NAME>FUNCT0</NAME> <CALLS> <FUNCTION> <NAME>FUNCT0_0</NAME> </FUNCTION>
<?xml version="1.0"?>
<CONFIG>
<FUNCTION>
<NAME>FUNCT0</NAME>
<CALLS>
<FUNCTION>
<NAME>FUNCT0_0</NAME>
</FUNCTION>
</CALLS>
<CALLS>
<FUNCTION>
<NAME>FUNCT0_1</NAME>
</FUNCTION>
</CALLS>
</FUNCTION>
<FUNCTION>
<NAME>FUNCT1</NAME>
</FUNCTION>
</CONFIG>
职能
函数0
功能1
功能1
我有一个名为FunctionInfo的类,它存储函数的名称,还包含一个ArrayList来包含函数调用的子函数
我希望最终得到一个ArrayList,其中包含顶级函数,这些函数将递归地将它们的子函数存储在对象中
import java.io.IOException;
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 RecursiveDOM {
public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
new RecursiveDOM("file.xml");
}
public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
final Document doc = docBuilder.parse(this.getClass().getResourceAsStream(file));
final List<String> l = new ArrayList<String>();
parse(doc, l, doc.getDocumentElement());
System.out.println(l);
}
private void parse(final Document doc, final List<String> list, final Element e) {
final NodeList children = e.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
list.add(n.getNodeName());
parse(doc, list, (Element) n);
}
}
}
}
我需要它来处理无限深度的递归
我的问题是,编写能够执行此任务的递归XML解析器的最简单方法是什么
编辑:我在Java工作
谢谢:)除非文件很大,否则可以使用java DOM解析器(DOM解析器将文件保存在内存中) 给定一个节点(从根开始),可以枚举其子节点,然后递归地对每个子节点调用相同的函数
import java.io.IOException;
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 RecursiveDOM {
public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
new RecursiveDOM("file.xml");
}
public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
final Document doc = docBuilder.parse(this.getClass().getResourceAsStream(file));
final List<String> l = new ArrayList<String>();
parse(doc, l, doc.getDocumentElement());
System.out.println(l);
}
private void parse(final Document doc, final List<String> list, final Element e) {
final NodeList children = e.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node n = children.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
list.add(n.getNodeName());
parse(doc, list, (Element) n);
}
}
}
}
也可以考虑使用JAXB(映射类到XML),这样会生成一个java类树,如果实现正确,每个类都可以实现特定类型的函数,这样树可以递归地“计算”。 这是一个基本的JAXB实现,请使用Config.unmarshal从InputStream重建它:
import java.io.OutputStream;
import java.io.Reader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class TestJAXB {
public static void main(final String[] args) throws JAXBException {
final Function e5 = new Function("N5", null);
final Function e6 = new Function("N6", null);
final Function e4 = new Function("N4", null);
final Function e2 = new Function("N2", ((new Call[] { new Call(e4) })));
final Function e3 = new Function("N3", ((new Call[] { new Call(e5) })));
final Function e1 = new Function("N1", ((new Call[] { new Call(e2), new Call(e3) })));
new Config(new Function[] { e1, e6 }).marshall(System.out);
}
@XmlRootElement(name = "CONFIG")
static class Config {
@XmlElement(name = "FUNCTION")
private Function[] functions;
public Config(final Function[] f) {
this.functions = f;
}
protected Config() {
}
public void marshall(final OutputStream out) throws JAXBException {
final JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass());
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(this, out);
}
public final static Config unmarshall(final Reader r) throws JAXBException {
final JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (Config) unmarshaller.unmarshal(r);
}
}
@XmlRootElement(name = "FUNCTION")
static class Function {
@XmlElement(name = "NAME")
String name;
@XmlElement(name = "CALLS")
Call[] calls;
public Function(final String name, final Call[] calls) {
this.name = name;
this.calls = calls;
}
protected Function() {
}
}
@XmlRootElement(name = "CALL")
static class Call {
@XmlElement(name = "FUNCTION")
Function f;
protected Call() {
}
public Call(final Function f) {
this.f = f;
}
}
}
基于你的问题,我假设这是用Java编写的?是的,很抱歉忘记提及。请阅读SAX事件驱动解析。很酷,谢谢。事实上我自己也做到了。不过还是谢谢你。