If you want any vqMod converted I can do them for you for a $5 for £3 payment emailed back to you just send paypal gift to info@worldprops.com and the file to zastil@gmail.com.
(it maybe need some changes for attributes I have missed)
Code: Select all
package vqMod2ocMod;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
ocmod objOcMod = new ocmod();
File fXmlFile = new File("OldFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("modification");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
//System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
objOcMod.setCode(eElement.getElementsByTagName("id").item(0).getTextContent().toString().toLowerCase().replace(" ", ""));
objOcMod.setAuthor(eElement.getElementsByTagName("author").item(0).getTextContent());
objOcMod.setName(eElement.getElementsByTagName("id").item(0).getTextContent());
objOcMod.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent());
objOcMod.setLink("http://www.zastil.co.uk");
}
}
// Now loop the file mods
Element eFiles = (Element) nList.item(0);
NodeList nListFiles = eFiles.getElementsByTagName("file");
// Loop the element files in XML
for (int temp1 = 0; temp1 < nListFiles.getLength(); temp1++) {
ocmodFile objOcModFile = new ocmodFile();
// Now grab oparations inside the file
Element eOperations = (Element) nListFiles.item(temp1);
NodeList nOperations = eOperations.getElementsByTagName("operation");
// Loop for all elements for operations
for (int temp2 = 0; temp2 < nOperations.getLength(); temp2++) {
// Get the node for current option
Node nOperation = nOperations.item(temp2);
// Also need it as Element for as find children serach and add
Element eOperation = (Element) nOperation;
// Set a new obj for Operation
ocmodOperation objOcmodOperation = new ocmodOperation();
// Find operation attributes
// here we also need the atrribute from search i.e replace and index
for (int temp4 = 0; temp4 < eOperation.getAttributes().getLength(); temp4++){
if (eOperation.getAttributes().item(temp4).getNodeName() == "error") objOcmodOperation.setError(eOperation.getAttributes().item(temp4).getTextContent());
}
// Grab the 2 nodes search and add
Node nSearch = eOperation.getElementsByTagName("search").item(0);
Node nAdd = eOperation.getElementsByTagName("add").item(0);
// here we also need the atrribute from search i.e replace and index
for (int temp3 = 0; temp3 < nSearch.getAttributes().getLength(); temp3++){
if (nSearch.getAttributes().item(temp3).getNodeName() == "position") objOcmodOperation.setPosition(nSearch.getAttributes().item(temp3).getTextContent());
if (nSearch.getAttributes().item(temp3).getNodeName() == "index") objOcmodOperation.setIndex(nSearch.getAttributes().item(temp3).getTextContent());
}
// Trim the search and add and set them to current operation object
objOcmodOperation.setSearch(nSearch.getTextContent().trim());
objOcmodOperation.setAdd(nAdd.getTextContent().trim());
// Now add the operation obj to the file obj
objOcModFile.setOperation(objOcmodOperation);
// Get the name value in vqMod and turn into Path only need to take item 0 as only 1 attribute in name field
objOcModFile.setPath(nListFiles.item(temp1).getAttributes().item(0).getTextContent());
}
objOcMod.setFile(objOcModFile);
}
WriteXMLFile writeXMLFile = new WriteXMLFile();
writeXMLFile.writeXML(objOcMod);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Code: Select all
package vqMod2ocMod;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
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.transform.stream.StreamSource;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteXMLFile {
public static void main(String argv[]) {
}
public void writeXML(ocmod objOcMod) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
// Remove Standalon
doc.setXmlStandalone(true);
Element rootElement = doc.createElement("modification");
doc.appendChild(rootElement);
// code element
Element code = doc.createElement("code");
rootElement.appendChild(code);
code.appendChild(doc.createTextNode(objOcMod.getCode()));
// name element
Element name = doc.createElement("name");
rootElement.appendChild(name);
name.appendChild(doc.createTextNode(objOcMod.getName()));
// version element
Element version = doc.createElement("version");
rootElement.appendChild(version);
version.appendChild(doc.createTextNode(objOcMod.getVersion()));
// author element
Element author = doc.createElement("author");
rootElement.appendChild(author);
author.appendChild(doc.createTextNode(objOcMod.getAuthor()));
// link element
Element link = doc.createElement("link");
rootElement.appendChild(link);
link.appendChild(doc.createTextNode(objOcMod.getLink()));
for (ocmodFile lstFile : objOcMod.getFiles()){
// file element
Element file = doc.createElement("file");
file.setAttribute("path", lstFile.getPath());
rootElement.appendChild(file);
for (ocmodOperation lstOperation: lstFile.getOperations()){
// operation element
Element operation = doc.createElement("operation");
// Add error attribute if exists
if (lstOperation.getError().length() > 0) operation.setAttribute("error", lstOperation.getError().toString());
file.appendChild(operation);
Element search = doc.createElement("search");
operation.appendChild(search);
search.appendChild(doc.createCDATASection(lstOperation.getSearch()));
Element add = doc.createElement("add");
operation.appendChild(add);
add.appendChild(doc.createCDATASection(lstOperation.getAdd()));
String strReplace = lstOperation.getPosition().toString();
if (strReplace.length() > 0) add.setAttribute("replace", strReplace);
if (lstOperation.getIndex().length() > 0) search.setAttribute("index", lstOperation.getIndex().toString());
}
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 2);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("NewFile.xml"));
// Output to console for testing
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
Code: Select all
package vqMod2ocMod;
import java.util.ArrayList;
import java.util.List;
public class ocmod {
private String code;
private String name;
private String version;
private String author;
private String link;
private List<ocmodFile> files = new ArrayList<ocmodFile>();
/**
* @return the code
*/
public String getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the version
*/
public String getVersion() {
return version;
}
/**
* @param version the version to set
*/
public void setVersion(String version) {
this.version = version;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the link
*/
public String getLink() {
return link;
}
/**
* @param link the link to set
*/
public void setLink(String link) {
this.link = link;
}
public List<ocmodFile> getFiles() {
return files;
}
public void setFiles(List<ocmodFile> files) {
this.files = files;
}
public void setFile(ocmodFile file) {
this.files.add(file);
}
public ocmodFile getOperation(int index) {
return this.files.get(index);
}
}
Code: Select all
package vqMod2ocMod;
import java.util.ArrayList;
import java.util.List;
public class ocmodFile {
private String path;
private List<ocmodOperation> operations = new ArrayList<ocmodOperation>();
public List<ocmodOperation> getOperations() {
return operations;
}
public void setOperations(List<ocmodOperation> operations) {
this.operations = operations;
}
public void setOperation(ocmodOperation operation) {
this.operations.add(operation);
}
public ocmodOperation getOperation(int index) {
return this.operations.get(index);
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Code: Select all
package vqMod2ocMod;
public class ocmodOperation {
private String search;
private String add;
private String position = "";
private String index = "";
private String error = "";
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}