从/ separate字符串创建XML

时间:2014-10-12 12:42:53

标签: java list compare

以下是Map元素 我需要从中创建一个xslt。

  {    /output1/output1/output1/output13=/input12/bunny,  
    /output1/output1/output1/output14=/input12/no,    
    /output1/output1/output13/output12=/input12/ok,   
    /output1/output1/output13/output16=/input12/honey

     }

我应该如何比较关键元素的每个元素与下一个关键元素的每个元素?

包mig;

import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;

import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Attr;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;

公共类CreateXSLT {

public static final String xmlFilePath = "D:/xmlfile.xml";

public void  createXSLT(ArrayList<String> SourceValues,ArrayList<String> SourceTypes,ArrayList<String> TargetValues,ArrayList<String> TargetTypes,ArrayList<String> Transform)
{


    try {

        Map<String, String> yourMap = new HashMap<String, String>();

        for(int x=0;x<TargetValues.size();x++)
        {
            yourMap.put(TargetValues.get(x),SourceValues.get(x));
        }

        Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);

        System.out.println(sortedMap);

        Set<Entry<String, String>> entires =sortedMap.entrySet();
        Set<String> keyset = sortedMap.keySet();
        System.out.println(keyset);


    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

    Document document = documentBuilder.newDocument();

    Element root = document.createElement("xsl:stylesheet");

    document.appendChild(root);

    Element root1 = document.createElement("xsl:template");
    root.appendChild(root1);

    Attr attr = document.createAttribute("match");
    attr.setValue("/");
    root1.setAttributeNode(attr);




    Element empDiffNxt = null; 
    Element  empDiff = null; 


int nodeLength = keyset.size(); 
System.out.println("TARGET sIZE: " + nodeLength);



    for(String key : keyset)
    {

        ArrayList<String> check = new ArrayList<String>();

        check.add(key);
        String[] Tg = key.split("/");



     for (int m=1; m< Tg.length; m++)

     {




         if( m == 1 )
         {
          empDiffNxt = document.createElement(Tg[m]);
         root1.appendChild(empDiffNxt);
         }
         else 
         {
              empDiff = document.createElement(Tg[m]);
              empDiffNxt.appendChild(empDiff);
              empDiffNxt= empDiff;
         }




     }

     Element emp1 = document.createElement("xsl:value-of");

        empDiffNxt.appendChild(emp1);

        Attr attr1 = document.createAttribute("select");


        attr1.setValue(sortedMap.get(key));

        emp1.setAttributeNode(attr1);



}






    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource domSource = new DOMSource(document);
    StreamResult streamResult = new StreamResult(new File(xmlFilePath));



    transformer.transform(domSource, streamResult);

    System.out.println("Done creating XML File");


    }
    catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}

}

这是我写的代码.....

此部分将作为Targetvalue内的列表

     /output1/output1/output1/output13           
    /output1/output1/output1/output14                  
    /output1/output1/output13/output12            
    /output1/output1/output13/output16 

    /input12/bunny
    /input12/bunny
    /input12/bunny
    /input12/bunny

列表中的源值

超值和目标值有链接..表示源的第一个值与目标的第一个值相关联。第二个值与目标的第二个值连接。

  <?xml version="1.0" encoding="UTF-8" standalone="no"?><xsl:stylesheet><xsl:template match="/"><output1><output1><output1><output13><xsl:value-of select="/input12/bunny"/></output13></output1></output1></output1><output1><output1><output1><output14><xsl:value-of select="/input12/no"/></output14></output1></output1></output1><output1><output1><output13><output12><xsl:value-of select="/input12/ok"/><xsl:value-of select="/input12/honey"/></output12></output13></output1></output1></xsl:template></xsl:stylesheet>

这是生成.........

但我想要的是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet>
<xsl:template match="/">
<output1>
   <output1>
     <output1>
      <output13>
        <xsl:value-of select="/input12/bunny"/>
      </output13>
      <output14>
        <xsl:value-of select="/input12/no"/>
      </output14>
     </output1>

     <output13>
       <output12>
         <xsl:value-of select="/input12/ok"/>
       </output12>
       <output16>
         <xsl:value-of select="/input12/honey"/>
       </output16>
     </output13>

   </output1>
 </output1>

0 个答案:

没有答案