1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
public class ParseXMLFile2 {
private final static String xmlFileName = "desc2007.xml";
/** Creates a new instance of ParseXMLFile */
public ParseXMLFile2() {
// parse XML file -> XML document will be build
Document doc = parseFile(xmlFileName);
// get root node of xml tree structure
Node root = doc.getDocumentElement();
// write node and its child nodes into CSV file
writeDocumentToFile(root,0);
}
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue( Node elem ) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}
private String getIndentSpaces(int indent) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < indent; i++) {
buffer.append(" ");
}
return buffer.toString();
}
/** Writes node and all child nodes into CSV file
* @param node XML node from from XML tree wrom which will output statement start
*/
public void writeDocumentToFile(Node node, int indent) {
// get element name
String nodeName = node.getNodeName();
// get element value
String nodeValue = getElementValue(node);
// get attributes of element
NamedNodeMap attributes = node.getAttributes();
try {
BufferedWriter out = new BufferedWriter(new FileWriter("Descriptors.csv",true));
out.write(getIndentSpaces(indent) + nodeName + "," + nodeValue);
out.newLine();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
out.write(getIndentSpaces(indent + 3) + attribute.getNodeName() + "," + attribute.getNodeValue());
out.newLine();
}
// write all child nodes recursively
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
writeDocumentToFile(child, indent + 3);
}
}
out.close();
} catch (IOException e) {
System.out.println("IOException:");
e.printStackTrace();
}
}
/** Parses XML file and returns XML document.
* @param fileName XML file to parse
* @return XML document or <B>null</B> if error occured
*/
public Document parseFile(String fileName) {
System.out.println("Parsing XML file... " + fileName);
DocumentBuilder docBuilder;
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
return null;
}
File sourceFile = new File(fileName);
try {
doc = docBuilder.parse(sourceFile);
}
catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
}
catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
}
System.out.println("XML file parsed");
return doc;
}
public static void main(String[] args) {
new ParseXMLFile2();
}
} |
Partager