Java 从HashMap创建XML输入数据
我在HashMap中有以下数据:Java 从HashMap创建XML输入数据,java,xml,dynamic,Java,Xml,Dynamic,我在HashMap中有以下数据: Map<String, String> data = new HashMap<String, String>(); data.put("A.B.C.C1.C11", "C11"); data.put("A.B.C.C1.C12", "C12"); data.put("A.B.C.C2.C21", "C21"); data.put("A.B.C.C2.C22", "C22")
Map<String, String> data = new HashMap<String, String>();
data.put("A.B.C.C1.C11", "C11");
data.put("A.B.C.C1.C12", "C12");
data.put("A.B.C.C2.C21", "C21");
data.put("A.B.C.C2.C22", "C22");
data.put("A.B.C.C3.C31", "C31");
data.put("A.B.C.D", "D");
Map data=newhashmap();
数据输入(“A.B.C.C1.C11”、“C11”);
数据输入(“A.B.C.C1.C12”、“C12”);
数据出售(“A.B.C.C2.C21”、“C21”);
数据出售(“A.B.C.C2.C22”、“C22”);
数据输入(“A.B.C.C3.C31”、“C31”);
数据。投入(“A.B.C.D”、“D”);
我需要创建以下格式的XML(预期输出):
C11
C12
C21
C22
C3
D
生成以下输出代码:
<A>
<B>
<C>
<D>
<C1>
<C11>
<C12>
<C2>
<C21>
<C22>
<C3>
<C31/>
</C3>
</C22>
</C21>
</C2>
</C12>
</C11>
</C1>
</D>
</C>
</B>
</A>
我写的代码(脏的):
public class DynamicXML3 {
public static boolean isFirstElemnt=true;
static Element element = null;
public static Element mainRoot =null;
public static void main(String args[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();
Document document = builder.newDocument();
String root,a1="a1";
Map<String, String> data = new HashMap<String, String>();
data.put("A.B.C.C1.C11", "C11");
data.put("A.B.C.C1.C12", "C12");
data.put("A.B.C.C2.C21", "C21");
data.put("A.B.C.C2.C22", "C22");
data.put("A.B.C.C3.C31", "C31");
data.put("A.B.C.C.D", "D");
data = new TreeMap<String, String>(data);
Set set1 = data.entrySet();
Iterator i1 = set1.iterator();
while(i1.hasNext()) {
Map.Entry me1 = (Map.Entry)i1.next();
System.out.println(me1.getKey() + ": ");
//System.out.println(me1.getValue());
//create Buddy XML
String key= me1.getKey()+"";
String[] buddyXML= key.split("\\.");
NodeList rootElementList = document.getElementsByTagName(buddyXML[0]);
if(!(rootElementList.getLength()>0)){
mainRoot = document.createElement(buddyXML[0]);
document.appendChild(mainRoot);
System.out.println("root created\n");
}
for(int i=0; i<buddyXML.length; i++){
System.out.println("value: "+buddyXML[i]);
NodeList elementList = document.getElementsByTagName(buddyXML[i]);
System.out.println("here "+elementList.getLength());
System.out.println("creating Node "+buddyXML[i]);
if(!(elementList.getLength()>0)){
System.out.println("creating Node "+buddyXML[i] + " & main root: "+mainRoot.getNodeName());
Element element =createNode(buddyXML[i], document, mainRoot);
mainRoot = element;
}
}
System.out.println("");
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
// Add indentation to outputs
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("D://RC/BuddyCode.xml"));
transformer.transform(source, result);
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
public static void createElement(Element elemnetName, Document document, Object data) {
Text text =document.createTextNode(data.toString());
elemnetName.appendChild(text);
}
private static Element createNode(String nodeName, Document document,Element rootElement) {
Element node = document.createElement(nodeName);
rootElement.appendChild(node);
return node;
}
}
公共类动态CXML3{
公共静态布尔值isFirstElemnt=true;
静态元素=null;
公共静态元素mainRoot=null;
公共静态void main(字符串参数[]){
试一试{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=docFactory.newDocumentBuilder();
Document Document=builder.newDocument();
字符串根,a1=“a1”;
映射数据=新的HashMap();
数据输入(“A.B.C.C1.C11”、“C11”);
数据输入(“A.B.C.C1.C12”、“C12”);
数据出售(“A.B.C.C2.C21”、“C21”);
数据出售(“A.B.C.C2.C22”、“C22”);
数据输入(“A.B.C.C3.C31”、“C31”);
数据。投入(“A.B.C.C.D”、“D”);
数据=新树形图(数据);
Set set1=data.entrySet();
迭代器i1=set1.Iterator();
while(i1.hasNext()){
Map.Entry me1=(Map.Entry)i1.next();
System.out.println(me1.getKey()+“:”);
//System.out.println(me1.getValue());
//创建好友XML
字符串key=me1.getKey()+“”;
字符串[]buddyXML=key.split(\\);
NodeList rootElementList=document.getElementsByTagName(buddyXML[0]);
如果(!(rootElementList.getLength()>0)){
mainRoot=document.createElement(buddyXML[0]);
document.appendChild(mainRoot);
System.out.println(“根创建的\n”);
}
对于(int i=0;i0)){
System.out.println(“创建节点”+buddyXML[i]+”&main root:+mainRoot.getNodeName());
Element=createNode(buddyXML[i],文档,mainRoot);
mainRoot=元素;
}
}
System.out.println(“”);
}
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer Transformer=t工厂.newTransformer();
//向输出添加缩进
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}缩进金额,“2”);
DOMSource=新的DOMSource(文档);
StreamResult=newstreamresult(新文件(“D://RC/BuddyCode.xml”);
变换(源、结果);
}捕获(例外e){
System.out.println(“异常:+e”);
}
}
公共静态void createElement(元素elemnetName、文档文档、对象数据){
Text Text=document.createTextNode(data.toString());
elemnetName.appendChild(文本);
}
私有静态元素createNode(字符串nodeName、文档文档、元素rootElement){
元素节点=document.createElement(节点名);
appendChild(节点);
返回节点;
}
}
这不是预期的工作,我试图改变一些其他方法,但似乎不起作用,有人可以指导我在同一方面
注意:HashMap中的数据可能在水平和垂直方向上增加,这意味着在运行时我可以有任意数量的节点,因此要求创建一个通用代码。首选使用DomParser。在函数调用的代码中是这样做的
Element=createNode(buddyXML[I],document,mainRoot)代码>和功能声明作为createElement(元素elemnetName、文档文档、对象数据)
参数的顺序错误。这可能是造成问题的原因。我不清楚你的问题。但我发现这会导致代码出现问题。就这样贴出来了。如果您需要任何进一步的帮助,请告诉我。我们可以尝试以下方法吗
public class DynamicXML3 {
private static final String SEPARTOR = "\\.";
public static void main(String args[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();
Document document = builder.newDocument();
Map<String, String> data = new HashMap<String, String>();
data.put("A.B.C.C1.C11", "C11");
data.put("A.B.C.C1.C12", "C12");
data.put("A.B.C.C2.C21", "C21");
data.put("A.B.C.C2.C22", "C22");
data.put("A.B.C.C3.C31", "C31");
data.put("A.B.C.C.D", "D");
Iterator<Entry<String, String>> it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
appendElement(document, pair.getKey(), pair.getValue());
it.remove();
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("C:/Users/Desktop/BuddyCode.xml"));
transformer.transform(source, result);
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
private static void appendElement(Document document, String key, String value) {
String[] splitedElements = key.split(SEPARTOR);
for (int i = 0; i < splitedElements.length; i++) {
NodeList nlst = document.getElementsByTagName(splitedElements[i]);
if(nlst == null || nlst.getLength() == 0) {
if(i==0) {
document.appendChild(document.createElement(splitedElements[i]));
} else {
(document.getElementsByTagName(splitedElements[i-1]).item(0)).appendChild(document.createElement(splitedElements[i]));
}
if(i==(splitedElements.length-1)) {
(document.getElementsByTagName(splitedElements[i]).item(0)).appendChild(document.createTextNode(value));
}
}
}
}
}
公共类动态CXML3{
私有静态最终字符串分隔符=“\\”;
公共静态void main(字符串参数[]){
试一试{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=docFactory.newDocumentBuilder();
Document Document=builder.newDocument();
映射数据=新的HashMap();
数据输入(“A.B.C.C1.C11”、“C11”);
数据输入(“A.B.C.C1.C12”、“C12”);
数据出售(“A.B.C.C2.C21”、“C21”);
数据出售(“A.B.C.C2.C22”、“C22”);
数据输入(“A.B.C.C3.C31”、“C31”);
数据。投入(“A.B.C.C.D”、“D”);
迭代器it=data.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
System.out.println(pair.getKey()+“=”+pair.getValue());
appendElement(文档,pair.getKey(),pair.getValue());
it.remove();
}
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer Transformer=t工厂.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,
public class DynamicXML3 {
private static final String SEPARTOR = "\\.";
public static void main(String args[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();
Document document = builder.newDocument();
Map<String, String> data = new HashMap<String, String>();
data.put("A.B.C.C1.C11", "C11");
data.put("A.B.C.C1.C12", "C12");
data.put("A.B.C.C2.C21", "C21");
data.put("A.B.C.C2.C22", "C22");
data.put("A.B.C.C3.C31", "C31");
data.put("A.B.C.C.D", "D");
Iterator<Entry<String, String>> it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
appendElement(document, pair.getKey(), pair.getValue());
it.remove();
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("C:/Users/Desktop/BuddyCode.xml"));
transformer.transform(source, result);
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
private static void appendElement(Document document, String key, String value) {
String[] splitedElements = key.split(SEPARTOR);
for (int i = 0; i < splitedElements.length; i++) {
NodeList nlst = document.getElementsByTagName(splitedElements[i]);
if(nlst == null || nlst.getLength() == 0) {
if(i==0) {
document.appendChild(document.createElement(splitedElements[i]));
} else {
(document.getElementsByTagName(splitedElements[i-1]).item(0)).appendChild(document.createElement(splitedElements[i]));
}
if(i==(splitedElements.length-1)) {
(document.getElementsByTagName(splitedElements[i]).item(0)).appendChild(document.createTextNode(value));
}
}
}
}
}
static void main(String[] args) throws Exception {
Map<String,String> data = new HashMap<String, String>();
data.put("A.B.C.C1.C11", "C11");
data.put("A.B.C.C1.C12", "C12");
data.put("A.B.C.C2.C21", "C21");
data.put("A.B.C.C2.C22", "C22");
data.put("A.B.C.C3.C31", "C31");
data.put("A.B.C.C.D", "D"); // should probably be data.put("A.B.C.D", "D");
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder db = f.newDocumentBuilder();
Document d = db.newDocument();
for (Map.Entry<String, String> entry : data.entrySet() ) {
Node n = d;
String[] keys = entry.getKey().split("\\.");
for (String k : keys) {
NodeList nodes = d.getElementsByTagName(k);
if ( nodes.getLength() == 0 ) {
n.appendChild(n = d.createElement(k));
} else {
n = nodes.item(0);
}
}
n.appendChild(d.createTextNode(entry.getValue()));
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.transform(new DOMSource(d), new StreamResult(System.out));
}