我有一些XML(超過150個標籤)。
我需要基於該模板創建額外的50個xml文件,但需要使用另一個標記值。
所以我產生了隨機數據(保存爲xls,csv,sql格式),我需要在模板中填充該值並用新名稱保存。 我該怎麼做? (UNIX shell腳本,JAVA,軟件等)
感謝從模板生成XML並填充新數據
0
A
回答
0
假設你能夠使用SQL Server - 檢查出XML
http://msdn.microsoft.com/en-us/library/ms345137%28v=sql.90%29.aspx#forxml2k5_topic4
0
@BonyT,謝謝,你告訴我正確的方向,我正在使用db2,所以對我來說,答案是:
db2 => EXPORT TO "c:\temp\xml.del" OF DEL XML TO "c:\temp\xml" XMLFILE "xml" MESSAGES "c:\temp\msg.txt" SELECT * FROM TMP.XML_TEMPLATE
其中xml.del
(所有產生的個XML保存在一個文件c:\temp\xml\tmp_xml2.001.xml
- 我沒有找到方法來單獨保存個XML)
6,"<XDS FIL='tmp_xml2.001.xml' OFF='0' LEN='34780' />"
5,"<XDS FIL='tmp_xml2.001.xml' OFF='34780' LEN='34780' />"
7,"<XDS FIL='tmp_xml2.001.xml' OFF='69560' LEN='34780' />"
0
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.poi.common.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
public class XMLConversion {
private static void buildEntryList(List<String> entries,
String parentXPath, Element parent) {
NamedNodeMap attrs = parent.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = (Attr) attrs.item(i);
// TODO: escape attr value
entries.add(parentXPath + "[@" + attr.getName() + "='"
+ attr.getValue().trim() + "']");
}
HashMap<String, Integer> nameMap = new HashMap<String, Integer>();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child instanceof Text) {
// TODO: escape child value
entries.add(parentXPath + "='"
+ ((Text) child).getData().trim() + "'");
} else if (child instanceof Element) {
String childName = child.getNodeName();
Integer nameCount = nameMap.get(childName);
nameCount = nameCount == null ? 1 : nameCount + 1;
nameMap.put(child.getNodeName(), nameCount);
buildEntryList(entries, parentXPath + "/" + childName + "["
+ nameCount + "]", (Element) child);
}
}
}
public static List<String> getEntryList(Document doc) {
ArrayList<String> entries = new ArrayList<String>();
Element root = doc.getDocumentElement();
entries.add("/" + root.getNodeName() + "[1]" + "='" + "'");
buildEntryList(entries, "/" + root.getNodeName() + "[1]", root);
return entries;
}
public int createExcelForXML(Set<String> xPathSet,String targetExcelFileName,String fileTestCaseName) throws IOException{
HSSFWorkbook xmlWorkBook = new HSSFWorkbook();
HSSFSheet xmlSheet = xmlWorkBook
.createSheet("XML Unit Testing Template");
int excelCellNum=0;
HSSFRow xmlSheetRow = null;
HSSFCell xmlRowCell = null;
String[] headerRowElements = {fileTestCaseName+"_TC1","XPaths"};
for(int excelRowNum=0;excelRowNum<=1;excelRowNum++){
if(excelRowNum==0){
excelCellNum=2;
}
else
excelCellNum=1;
xmlSheetRow = xmlSheet.createRow(excelRowNum);
xmlRowCell = xmlSheetRow.createCell(excelCellNum);
xmlRowCell.setCellValue(headerRowElements[excelRowNum]);
}
int excelRowNum=2;
// for(int excelRowNum=2;excelRowNum<=xPathSet.size();excelRowNum++){
for(String xPaths : xPathSet){
int currentCellNum=1;
String xmlXpath=null;
String xmlTextContent=null;
xmlSheetRow = xmlSheet.createRow(excelRowNum);
xmlXpath=xPaths.substring(0,xPaths.indexOf("="));
xmlTextContent=xPaths.substring(xPaths.indexOf("=")+2,xPaths.lastIndexOf("'"));
for(int currCellNum=1;currCellNum<=2;currCellNum++){
if(currCellNum==1){
xmlRowCell = xmlSheetRow.createCell(currCellNum);
xmlRowCell.setCellValue(xmlXpath);
}
else{
xmlRowCell = xmlSheetRow.createCell(currCellNum);
xmlRowCell.setCellValue(xmlTextContent);
}
}
excelRowNum++;
}
String tempExcelPath=new File(targetExcelFileName).getParent();
System.out.println(tempExcelPath);
tempExcelPath=tempExcelPath.substring(6,tempExcelPath.length());
System.out.println(tempExcelPath);
String targetExcelFullPath = tempExcelPath+"\\"+fileTestCaseName+".xls";
FileOutputStream excelOutput = new FileOutputStream(new File(targetExcelFullPath));
xmlWorkBook.write(excelOutput);
System.out.println("done");
return 1;
}
public boolean parseXMLandCreateExcel(String fileName) throws Exception{
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
docFactory.setNamespaceAware(false);
int fileIndex=0;
String fileTestCaseName=null;
String targetPathName=null;
int isFileXML=0;
if(fileName.endsWith(".xml")){
isFileXML=1;
}
if(isFileXML!=0){
if(fileName.contains("\\")){
fileIndex=fileName.lastIndexOf("\\")+1;
}}
else
throw new Exception("You have not provided an XML to parse it. Please provide an XML file");
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.parse(new File(fileName));
fileTestCaseName=fileName.substring(fileIndex, fileName.lastIndexOf("."));
targetPathName=doc.getBaseURI();
// targetPathName=targetPathName.substring(0,doc.getBaseURI().lastIndexOf("."));
if(fileTestCaseName==null){
throw new Exception("The XML Conversion has encountered an error in getting the FileName for the Excel to be generated. Please try to resolve it.");
}
if(targetPathName==null){
throw new Exception("The XML Conversion has encountered an error in getting the Target Path for the Excel to be generated. Please try to resolve it.");
}
List<String> xmlList = new ArrayList<String>();
xmlList = getEntryList(doc);
Set<String> linkedSet = new LinkedHashSet<String>(xmlList);
/*for (String xmlL : linkedSet) {
System.out.println(xmlL);
}
*/
System.out.println();
System.out.println(targetPathName);
createExcelForXML(linkedSet,targetPathName,fileTestCaseName);
return true;
}
public static void main(String[] args){
String fileName = "sample1.xml";
//System.out.println(fileName.substring(fileIndex, fileName.lastIndexOf(".")));
XMLConversion parseXMLDocument=new XMLConversion();
try {
parseXMLDocument.parseXMLandCreateExcel(fileName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
enter code here
+0
你應該有一些評論與你的代碼一起去幫助人們理解你在做什麼。 –
+0
今天我將添加評論.. – Gowtham
0
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.poi.common.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
public class XMLConversion {
private static void buildEntryList(List<String> entries,
String parentXPath, Element parent) {
NamedNodeMap attrs = parent.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attr = (Attr) attrs.item(i);
// TODO: escape attr value
entries.add(parentXPath + "[@" + attr.getName() + "='"
+ attr.getValue().trim() + "']");
}
HashMap<String, Integer> nameMap = new HashMap<String, Integer>();
NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child instanceof Text) {
// TODO: escape child value
entries.add(parentXPath + "='"
+ ((Text) child).getData().trim() + "'");
} else if (child instanceof Element) {
String childName = child.getNodeName();
Integer nameCount = nameMap.get(childName);
nameCount = nameCount == null ? 1 : nameCount + 1;
nameMap.put(child.getNodeName(), nameCount);
buildEntryList(entries, parentXPath + "/" + childName + "["
+ nameCount + "]", (Element) child);
}
}
}
public static List<String> getEntryList(Document doc) {
ArrayList<String> entries = new ArrayList<String>();
Element root = doc.getDocumentElement();
entries.add("/" + root.getNodeName() + "[1]" + "='" + "'");
buildEntryList(entries, "/" + root.getNodeName() + "[1]", root);
return entries;
}
public int createExcelForXML(Set<String> xPathSet,String targetExcelFileName){
HSSFWorkbook xmlWorkBook = new HSSFWorkbook();
HSSFSheet xmlSheet = xmlWorkBook
.createSheet("XML Unit Testing Template");
int excelCellNum=0;
HSSFRow xmlSheetRow = null;
HSSFCell xmlRowCell = null;
String[] headerRowElements = {targetExcelFileName+"_TC1","XPaths"};
for(int excelRowNum=0;excelRowNum<=1;excelRowNum++){
if(excelRowNum==0){
excelCellNum=2;
}
else
excelCellNum=1;
xmlSheetRow = xmlSheet.createRow(excelRowNum);
xmlRowCell = xmlSheetRow.createCell(excelCellNum);
xmlRowCell.setCellValue(headerRowElements[excelRowNum]);
}
int excelRowNum=2;
// for(int excelRowNum=2;excelRowNum<=xPathSet.size();excelRowNum++){
for(String xPaths : xPathSet){
int currentCellNum=1;
String xmlXpath=null;
String xmlTextContent=null;
xmlSheetRow = xmlSheet.createRow(excelRowNum);
xmlXpath=xPaths.substring(0,xPaths.indexOf("="));
xmlTextContent=xPaths.substring(xPaths.indexOf("="),xPaths.lastIndexOf("'"));
for(int currCellNum=1;currCellNum<=2;currCellNum++){
if(currCellNum==1){
xmlRowCell = xmlSheetRow.createCell(currCellNum);
xmlRowCell.setCellValue(xmlXpath);
}
else{
xmlRowCell = xmlSheetRow.createCell(currCellNum);
xmlRowCell.setCellValue(xmlTextContent);
}
excelRowNum++;
}
}
//FileOutputS
return 1;
}
public boolean parseXMLandCreateExcel(String fileName) throws Exception{
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
docFactory.setNamespaceAware(false);
int fileIndex=0;
String fileTestCaseName=null;
String targetPathName=null;
int isFileXML=0;
if(fileName.endsWith(".xml")){
isFileXML=1;
}
if(isFileXML!=0){
if(fileName.contains("\\")){
fileIndex=fileName.lastIndexOf("\\")+1;
}}
else
throw new Exception("You have not provided an XML to parse it. Please provide an XML file");
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.parse(new File(fileName));
fileTestCaseName=fileName.substring(fileIndex, fileName.lastIndexOf("."));
targetPathName=doc.getBaseURI();
targetPathName=targetPathName.substring(0,doc.getBaseURI().lastIndexOf("."));
if(fileTestCaseName==null){
throw new Exception("The XML Conversion has encountered an error in getting the FileName for the Excel to be generated. Please try to resolve it.");
}
if(targetPathName==null){
throw new Exception("The XML Conversion has encountered an error in getting the Target Path for the Excel to be generated. Please try to resolve it.");
}
List<String> xmlList = new ArrayList<String>();
xmlList = getEntryList(doc);
Set<String> linkedSet = new LinkedHashSet<String>(xmlList);
/*for (String xmlL : linkedSet) {
System.out.println(xmlL);
}
*/
System.out.println();
System.out.println(targetPathName);
return true;
}
public static void main(String[] args){
String fileName = "sample1.xml";
//System.out.println(fileName.substring(fileIndex, fileName.lastIndexOf(".")));
XMLConversion parseXMLDocument=new XMLConversion();
try {
parseXMLDocument.parseXMLandCreateExcel(fileName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關問題
- 1. 在模板中填充表單並更新新數據
- 2. 從DataGridView提取數據並填充XML
- 3. 如何用數據填充生成的樞軸元素[模板]
- 4. 填充數據集從XML
- 5. 從ajax調用獲取數據並填充模板
- 6. 循環生成div並從數據庫填充它們
- 7. 從模板和XML數據生成word文件
- 8. Jquery模板來填充META數據?
- 9. 從XML模板的PDF生成器?
- 10. Dojo模板手動實例化並用JSON數據填充
- 11. 模式:從參數填充實例並將其導出到XML
- 12. 填充處理XML數據
- 13. 用XML數據填充UITableView
- 14. Jqgrid xml數據填充
- 15. 填充Moustache.js模板
- 16. 複製表數據並填充新列
- 17. 生成填充geom_step
- 18. 閱讀從XML文件中的數據並填充到Oracle表
- 19. 生成文件並填充值
- 20. 與數據從模板填充一個Django的形式動態
- 21. 填充從數據表中的頁面模式面板
- 22. 複製模板的工作表,並在另一片從數據表中填充
- 23. 將XML數據填充到數據集
- 24. 從xml填充gridview
- 25. 從XML填充treeview
- 26. 從模塊中填充數據網格
- 27. 推Uiview並填充數據
- 28. 從外部api填充樹枝模板
- 29. 重新生成數據庫與從模型生成的表
- 30. ios - 生成從CoreData填充的PDF
我認爲這將是非常複雜的,但我會嘗試 – Vitaliy
只有像您的要求一樣複雜 - 先嚐試一些簡單的操作,例如SELECT * FROM Customers FOR XML AUTO,返回XML數據類型的TYPE或SELECT * FROM Customers FOR XML AUTO,返回XML內容作爲varchar – BonyT
似乎我的問題類似於這個http://stackoverflow.com/questions/4267427/transform-xml-based-on-a-template-with-custom-logic-which-framework-how-to-achi – Vitaliy