所以基本上,我在转过头时遇到了问题。
基本上,我可以有任何类型的 xml.. 我必须解析它。
我能够成功地将它解析为“平面”xml。例如:
<emp>
<id>1</id>
<name>foo</name>
<age>22</age>
</emp>
我的简单解析器适用于此(请注意,架构是可变的..任何平面 xml(无硬编码)..
但是嵌套的xml东西失败了所以
<emplist>
<emp>
<manager>
<id>1</id>
<name>foo</name>
</manager>
</emp>
<emp>
<clerk>
<cid>1</cid>
<cname>foo</cname>
</clerk>
</emp>
</emplist>
我想要的第一种情况的输出(我得到的是)
id,1
name,foo
但第二次我想要
id, 1
name, foo
cid, 1
cname,foo
我如何把它弄平。谢谢
当前代码:
public class XMLReader {
public static void main(String[] args) throws JDOMException, IOException {
//String xmlString = "<employee >\n <firstname xml:space=\"preserve\" >John</firstname>\n <lastname>Watson</lastname>\n <age>30</age>\n <email>johnwatson@sh.com</email>\n</employee>";
String xmlString = "<employee>\n" +
" <personal><id>2D61EC47-0F56-5A33-6057-54DB0ABBDBF0</id>\n" +
" <name>Lareina</name>\n" +
" <age>50</age>\n" +
" </personal><contact><dept>Fusce</dept>\n" +
" <manager>B55E6DA8-76BD-A3C8-2DDF-686CB9A0BB76</manager></contact>\n" +
" </employee>";
System.out.println(xmlString);
SAXBuilder builder = new SAXBuilder();
Reader in = new StringReader(xmlString);
Document doc = builder.build(in);
Element root = doc.getRootElement();
List children = root.getChildren();
//System.out.println(children);
String value = "";
for (int i = 0; i < children.size(); i++) {
Element dataNode = (Element) children.get(i);
// Element dataNode = (Element) dataNodes.get(j);
value += ", " +dataNode.getText().trim();
System.out.println(dataNode.getName() + " : " + dataNode.getText());
//context.write(new Text(rowKey.toString()), new Text(node.getName().trim() + " " + node.getText().trim()));
}
//System.out.println(in);
}
}