xml解析集積
いくつかのxml解析のプログラムを貼って、後でよくめくります
package com.ibm.test;
import java.io.File;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomTest {
public static void main(String[] args) {
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document doc = saxBuilder.build(new File("domdb.xml"));
//
Element eltDb = new Element("db");
Element eltDriver = new Element("driver");
Element eltUrl = new Element("url");
Element eltUser = new Element("user");
Element eltPassword = new Element("password");
//
eltDriver.setText("com.mysql.jdbc.Driver");
eltUrl.setText("jdbc:mysql://localhost/mySql");
eltUser.setText("root");
eltPassword.setText("xlc");
//
eltDb.addContent(eltDriver);
eltDb.addContent(eltUrl);
eltDb.addContent(eltUser);
eltDb.addContent(eltPassword);
//
eltDb.setAttribute("type", "mysql");
Element root = doc.getRootElement();//This will return the root element for this document
// root.removeChild("db");//
root.addContent(eltDb);//
// db
root.getChild("db").getChild("user").setText("system");
root.getChild("db").getChild("password").setText("manager");
XMLOutputter xmlOut = new XMLOutputter();
// XML
Format fmt = Format.getPrettyFormat();
fmt.setIndent(" ");
fmt.setEncoding("utf-8");
xmlOut.setFormat(fmt);
xmlOut.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ibm.test;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
*
*
* @author xll
*
*/
public class Dom4jWrite {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
doc.addProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='db.xsl'");
doc.addDocType("dbconfig", null, "db.dtd");
// Element root=DocumentHelper.createElement("dbconfig");
// doc.setRootElement(root);
Element root = doc.addElement("dbconfig");
Element eltDb = root.addElement("db");
Element eltDriver = eltDb.addElement("driver");
Element eltUrl = eltDb.addElement("url");
Element eltUser = eltDb.addElement("user");
Element eltPassword = eltDb.addElement("password");
eltDriver.setText("com.mysql.jdbc.Driver");
eltUrl.setText("jdbc:mysql://localhost/mySql");
eltUser.setText("root");
eltPassword.setText("xlc");
eltDb.addAttribute("type", "mysql");
try {
//
OutputFormat outFmt = new OutputFormat(" ", true);
outFmt.setEncoding("UTF-8");
/**//*
* PrintWriter pw = new PrintWriter(System.out); doc.write(pw);
* pw.flush(); pw.close();
*/
// XMLWriter xmlWriter = new XMLWriter(System.out, outFmt);
XMLWriter xmlWriter = new XMLWriter(new FileWriter("db.xml"),
outFmt);
xmlWriter.write(doc);
xmlWriter.flush();
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.ibm.test;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jModify {
public Document modifyDocument(File inputXml) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
document.addDocType("dbconfig", null, "db.dtd");
List list = document.content();
// Iterator iter = document.nodeIterator();
Iterator iter = list.iterator();
Element element = (Element) iter.next();
element.element("db").attribute("type").setValue("mysql");
element.element("db").element("url").setText(
"jdbc:mysql://localhost/mySql");
element.element("db").element("driver").setText(
"com.mysql.jdbc.Driver");
element.element("db").element("user").setText("root");
element.element("db").element("password").setText("xlc");
//
OutputFormat outFmt = new OutputFormat(" ", true);
outFmt.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(new FileWriter(
"domdb-modified.xml"), outFmt);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
return document;
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
public static void main(String[] args) throws Exception {
Dom4jModify dom4jParser = new Dom4jModify();
Document document = dom4jParser.modifyDocument(new File("domdb.xml"));
OutputFormat outFmt = new OutputFormat(" ", true);
outFmt.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(System.out, outFmt);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
}
}
package com.ibm.xml;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
* Jdom
* @author xll
*
*/
public class JdomXmlDocument implements XmlDocument{
public void createXml(String fileName) {
Document document;
Element root;
root=new Element("employees");
document=new Document(root);
Element employee=new Element("employee");
root.addContent(employee);
Element name=new Element("name");
name.setText("ddvip");
employee.addContent(name);
Element sex=new Element("sex");
sex.setText("m");
employee.addContent(sex);
Element age=new Element("age");
age.setText("23");
employee.addContent(age);
XMLOutputter XMLOut = new XMLOutputter();
try {
XMLOut.output(document, new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void parserXml(String fileName) {
SAXBuilder builder=new SAXBuilder(false);
try {
Document document=builder.build(fileName);
Element employees=document.getRootElement();
List employeeList=employees.getChildren("employee");
//System.out.println("employeeList.size()" + employeeList.size());
for(Iterator iter = employeeList.iterator();iter.hasNext();){
Element employee=(Element)iter.next();
List employeeInfo=employee.getChildren();
//System.out.println(employeeInfo.size());
for(int i = 0;i < employeeInfo.size();i++){
System.out.println(((Element)employeeInfo.get(i)).getName()+":"+((Element)employeeInfo.get(i)).getValue());
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.ibm.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* Dom4j
*
* @author Administrator
*
*/
public class Dom4jXmlDocument implements XmlDocument {
public void createXml(String fileName) {
Document document = DocumentHelper.createDocument();
Element employees = document.addElement("employees");
Element employee = employees.addElement("employee");
Element name = employee.addElement("name");
name.setText("ddvip");
Element sex = employee.addElement("sex");
sex.setText("m");
Element age = employee.addElement("age");
age.setText("29");
try {
Writer fileWriter = new FileWriter(fileName);
XMLWriter xmlWriter = new XMLWriter(fileWriter);
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName) {
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext();) {
Element employee = (Element) i.next();
for (Iterator j = employee.elementIterator(); j.hasNext();) {
Element node = (Element) j.next();
System.out.println(node.getName() + ":" + node.getText());
}
}
} catch (DocumentException e) {
System.out.println(e.getMessage());
}
}
}