需要获取父 ID,但我在 Java 中获取 xml 标记的级别
Need to get the parent id but I'm getting level of the xml tag in Java
下面是我的XML
<jndi>
<ejbmodule>
<context>
<leaf>
<name>XAConnectionFactory</name>
<type>type1</type>
<year>2002</year>
<attribute name="class">org.jboss.mq.SpyXAConnectionFactory</attribute>
<test1>
<name>mytest</name>
</test1>
</leaf>
<name>java</name>
<type>type2</type>
<year>2003</year>
<leaf>
<name>DefaultDS</name>
<type>type3</type>
<year>2004</year>
<attribute name="class">javax.sql.DataSource</attribute>
</leaf>
</context>
</ejbmodule>
</jndi>
下面是我的 java 代码,我在其中遍历 XML 并将其转换为 DTree java 脚本代码以将 XML 显示为树结构在 html 页面上
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class RecursiveDOM {
static int globalCounter = 0;
public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
new RecursiveDOM("C:\Users\Rajani\Downloads\SampleXMLData\dTree_1\new1.xml");
}
public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
InputSource is = new InputSource(new FileReader(new File(file)));
final Document doc = docBuilder.parse(is);
StringBuffer html = new StringBuffer();
int parentId = -1;
html.append("<div class=\"dtree\"><script type=\"text/javascript\" src=\"dtree.js\"></script>\n");
html.append(createTreeCommandLinks("decisionReportTree"));
// create tree and add root node
html.append("<script type=\"text/javascript\">\n<!--\n");
html.append("decisionReportTree = new dTree('decisionReportTree');\n");
html.append("decisionReportTree.add(" + globalCounter++ + ",-1,'List Persons');\n");
traverse(doc, parentId, html);
html.append("document.write(decisionReportTree);");
html.append("\n//-->\n</script>");
System.out.println(html);
}
private static String add(String tree, int global, int parent, String name) {
return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n";
}
public static void traverse(Node node, int parentId, StringBuffer html) {
handle_node(node, parentId, html);
if (node.hasChildNodes()) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node kid = children.item(i);
if (kid.getNodeType() == Node.ELEMENT_NODE) {
traverse(kid, parentId + 1, html);
}
}
}
}
public static void handle_node(Node node, int parentId, StringBuffer html) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
String value = getNodeValue(node);
if(null != value && value.trim().length() > 0) {
html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName() + ":::" + value));
} else {
html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName()));
}
}
}
private static String createTreeCommandLinks(String treeName) {
return "<p><a href=\"javascript: " + treeName + ".openAll();\">Expands all</a> | <a href=\"javascript: " + treeName + ".closeAll();\">Collapse all</a></p>";
}
protected static String getNodeValue(Node node) {
NodeList childNodes = node.getChildNodes();
for (int x = 0; x < childNodes.getLength(); x++) {
Node data = childNodes.item(x);
if (data.getNodeType() == Node.TEXT_NODE)
return data.getNodeValue();
}
return "";
}
protected String getNodeAttrValue(String attrName, Node node) {
NamedNodeMap attrs = node.getAttributes();
for (int y = 0; y < attrs.getLength(); y++) {
Node attr = attrs.item(y);
if (attr.getNodeName().equalsIgnoreCase(attrName)) {
return attr.getNodeValue();
}
}
return "";
}
}
我使用上述程序得到的输出是
decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 5, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 4, 'name:::DefaultDS');
decisionReportTree.add(16, 4, 'type:::type3');
decisionReportTree.add(17, 4, 'year:::2004');
decisionReportTree.add(18, 4, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);
但我需要以下输出
decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 9, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 14, 'name:::DefaultDS');
decisionReportTree.add(16, 14, 'type:::type3');
decisionReportTree.add(17, 14, 'year:::2004');
decisionReportTree.add(18, 14, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);
问题出在这里:
traverse(kid, parentId + 1, html);
在您传递 parentId + 1
的地方,您应该提供您正在遍历的子节点的 ID。要获得它,将 handle_node
更改为 return 它分配给处理节点的 id ...
这里的错误是 traverse(kid, parentId + 1, html)
孩子。您这样做的方式是计算节点深度,而不是传输注释父亲 ID。
要获得期望的结果,您必须在每次交互时传递当前和 parent ID。看看这个例子 class:
public class Example {
static Node root = new Node("List Persons",
new Node("jndi",
new Node("ejbmodule",
new Node("context",
new Node("leaf",
new Node("name"),
new Node("type"),
new Node("year"),
new Node("attribute"),
new Node("test1", new Node("name"))
),
new Node("name"),
new Node("type"),
new Node("year"),
new Node("leaf",
new Node("name"),
new Node("type"),
new Node("year"),
new Node("attribute")
)
)
)
)
);
static int globalCounter = 0;
public static void main(String[] args) {
traverse(root, globalCounter++, -1, null);
}
private static String add(String tree, int global, int parent, String name) {
return tree + ".add(" + global + ", " + parent + ", '" + name + "');";
}
public static void traverse(Node node, int currentId, int parentId, StringBuffer html) {
handle_node(node, currentId, parentId, html);
//if (node.hasChildNodes()) {
//NodeList children = node.getChildNodes();
for (Node kid : node.childs) {
//Node kid = children.item(i);
//if (kid.getNodeType() == Node.ELEMENT_NODE) {
traverse(kid, globalCounter++ , currentId, html);
//}
}
//}
}
public static void handle_node(Node node, int currentId, int parentId, StringBuffer html) {
//if (node.getNodeType() == Node.ELEMENT_NODE) {
String value = node.value; //getNodeValue(node);
System.out.println(add("decisionReportTree", currentId, parentId, value ));
//if(null != value && value.trim().length() > 0) {
// html.append(add("decisionReportTree", currentId, parentId, node.getNodeName() + ":::" + value));
//} else {
// html.append(add("decisionReportTree", currentId, parentId, node.getNodeName()));
//}
//}
}
}
class Node {
String value;
Node[] childs;
public Node(String value, Node... childs) {
this.value = value;
this.childs = childs;
}
}
希望对您有所帮助!
下面是我的XML
<jndi>
<ejbmodule>
<context>
<leaf>
<name>XAConnectionFactory</name>
<type>type1</type>
<year>2002</year>
<attribute name="class">org.jboss.mq.SpyXAConnectionFactory</attribute>
<test1>
<name>mytest</name>
</test1>
</leaf>
<name>java</name>
<type>type2</type>
<year>2003</year>
<leaf>
<name>DefaultDS</name>
<type>type3</type>
<year>2004</year>
<attribute name="class">javax.sql.DataSource</attribute>
</leaf>
</context>
</ejbmodule>
</jndi>
下面是我的 java 代码,我在其中遍历 XML 并将其转换为 DTree java 脚本代码以将 XML 显示为树结构在 html 页面上
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class RecursiveDOM {
static int globalCounter = 0;
public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
new RecursiveDOM("C:\Users\Rajani\Downloads\SampleXMLData\dTree_1\new1.xml");
}
public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
InputSource is = new InputSource(new FileReader(new File(file)));
final Document doc = docBuilder.parse(is);
StringBuffer html = new StringBuffer();
int parentId = -1;
html.append("<div class=\"dtree\"><script type=\"text/javascript\" src=\"dtree.js\"></script>\n");
html.append(createTreeCommandLinks("decisionReportTree"));
// create tree and add root node
html.append("<script type=\"text/javascript\">\n<!--\n");
html.append("decisionReportTree = new dTree('decisionReportTree');\n");
html.append("decisionReportTree.add(" + globalCounter++ + ",-1,'List Persons');\n");
traverse(doc, parentId, html);
html.append("document.write(decisionReportTree);");
html.append("\n//-->\n</script>");
System.out.println(html);
}
private static String add(String tree, int global, int parent, String name) {
return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n";
}
public static void traverse(Node node, int parentId, StringBuffer html) {
handle_node(node, parentId, html);
if (node.hasChildNodes()) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node kid = children.item(i);
if (kid.getNodeType() == Node.ELEMENT_NODE) {
traverse(kid, parentId + 1, html);
}
}
}
}
public static void handle_node(Node node, int parentId, StringBuffer html) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
String value = getNodeValue(node);
if(null != value && value.trim().length() > 0) {
html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName() + ":::" + value));
} else {
html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName()));
}
}
}
private static String createTreeCommandLinks(String treeName) {
return "<p><a href=\"javascript: " + treeName + ".openAll();\">Expands all</a> | <a href=\"javascript: " + treeName + ".closeAll();\">Collapse all</a></p>";
}
protected static String getNodeValue(Node node) {
NodeList childNodes = node.getChildNodes();
for (int x = 0; x < childNodes.getLength(); x++) {
Node data = childNodes.item(x);
if (data.getNodeType() == Node.TEXT_NODE)
return data.getNodeValue();
}
return "";
}
protected String getNodeAttrValue(String attrName, Node node) {
NamedNodeMap attrs = node.getAttributes();
for (int y = 0; y < attrs.getLength(); y++) {
Node attr = attrs.item(y);
if (attr.getNodeName().equalsIgnoreCase(attrName)) {
return attr.getNodeValue();
}
}
return "";
}
}
我使用上述程序得到的输出是
decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 5, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 4, 'name:::DefaultDS');
decisionReportTree.add(16, 4, 'type:::type3');
decisionReportTree.add(17, 4, 'year:::2004');
decisionReportTree.add(18, 4, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);
但我需要以下输出
decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 9, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 14, 'name:::DefaultDS');
decisionReportTree.add(16, 14, 'type:::type3');
decisionReportTree.add(17, 14, 'year:::2004');
decisionReportTree.add(18, 14, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);
问题出在这里:
traverse(kid, parentId + 1, html);
在您传递 parentId + 1
的地方,您应该提供您正在遍历的子节点的 ID。要获得它,将 handle_node
更改为 return 它分配给处理节点的 id ...
这里的错误是 traverse(kid, parentId + 1, html)
孩子。您这样做的方式是计算节点深度,而不是传输注释父亲 ID。
要获得期望的结果,您必须在每次交互时传递当前和 parent ID。看看这个例子 class:
public class Example {
static Node root = new Node("List Persons",
new Node("jndi",
new Node("ejbmodule",
new Node("context",
new Node("leaf",
new Node("name"),
new Node("type"),
new Node("year"),
new Node("attribute"),
new Node("test1", new Node("name"))
),
new Node("name"),
new Node("type"),
new Node("year"),
new Node("leaf",
new Node("name"),
new Node("type"),
new Node("year"),
new Node("attribute")
)
)
)
)
);
static int globalCounter = 0;
public static void main(String[] args) {
traverse(root, globalCounter++, -1, null);
}
private static String add(String tree, int global, int parent, String name) {
return tree + ".add(" + global + ", " + parent + ", '" + name + "');";
}
public static void traverse(Node node, int currentId, int parentId, StringBuffer html) {
handle_node(node, currentId, parentId, html);
//if (node.hasChildNodes()) {
//NodeList children = node.getChildNodes();
for (Node kid : node.childs) {
//Node kid = children.item(i);
//if (kid.getNodeType() == Node.ELEMENT_NODE) {
traverse(kid, globalCounter++ , currentId, html);
//}
}
//}
}
public static void handle_node(Node node, int currentId, int parentId, StringBuffer html) {
//if (node.getNodeType() == Node.ELEMENT_NODE) {
String value = node.value; //getNodeValue(node);
System.out.println(add("decisionReportTree", currentId, parentId, value ));
//if(null != value && value.trim().length() > 0) {
// html.append(add("decisionReportTree", currentId, parentId, node.getNodeName() + ":::" + value));
//} else {
// html.append(add("decisionReportTree", currentId, parentId, node.getNodeName()));
//}
//}
}
}
class Node {
String value;
Node[] childs;
public Node(String value, Node... childs) {
this.value = value;
this.childs = childs;
}
}
希望对您有所帮助!