/** * 功能:开始写Readme文件 * 参数:strType:创建文件类型,文件可能是Anytime或Daily类型; * 返回值:无 */ public void startBuild(String strType) { this.m_strCurrentType = strType; Element elementCommon = null; Element elementRPS = null;
if (ANYTIME == strType) { m_elementRoot = new Element("Anytime"); elementCommon = new Element("Common"); elementRPS = new Element("RPS"); } else if (DAILY == strType) { m_elementRoot = new Element("Daily"); elementCommon = new Element("Common"); elementRPS = new Element("RPS"); } m_elementRoot.addContent(elementCommon); m_elementRoot.addContent(elementRPS); }
if (!hostList.isEmpty()) { for (int i = 0; i < size; i++) { Element element = (Element)hostList.get(i); if (strAttributeValue.equalsIgnoreCase(element.getAttributeValue("IP"))) { System.out.println("in"); System.out.println(element.getName()); return element; } } } System.out.println("out"); return null; }
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var root;
var newNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
root = xmlDoc.documentElement;
alert(root.xml);
newNode = xmlDoc.createNode(1, "VIDEOS", "");
root.appendChild(newNode);
alert(root.xml);这是javascript的
首先
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;写入xml文件代码片段
Element root=new Element("root");
Element aElement=new Element("a").addContent("test");
Element bElement=new Element("b");
bElement.addContent(new Element("1").addContent("1"));
bElement.addContent(new Element("2").addContent("2"));
bElement.addContent(new Element("3").addContent("3"));
bElement.addContent(new Element("4").addContent("4"));
root.addContent(aElement);
root.addContent(bElement);
Document doc=new Document(root);
XMLOutputter xmlOut=new XMLOutputter(" ",true);
xmlOut.setEncoding("ISO8859-1");
try{
FileOutputStream output=new FileOutputStream("test.xml");
xmlOut.output(document,output);
}catch(Exception e){}
修改xml文档并写回磁盘的代码片段
DOMBuilder builder=new DOMBuilder();
Document document=builder.build(new File("test.xml"));
Element root=document.getRootElement();
Element four=root.getChild("b").getChild("4");
four.setText("5");
XMLOutputter xmlOut=new XMLOutputter(" ",true);
xmlOut.setEncoding("ISO8859-1");
try{
FileOutputStream output=new FileOutputStream("test.xml");
xmlOut.output(document,output);
}catch(Exception e){}
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Domxml{
public static void main(String args[]){
//org.w3c.dom.Document
Document doc;
//org.w3c.dom.Element
Element people =null,
person =null,
name =null,
address =null,
tel =null,
fax =null,
email =null;
try{
//javax.xml.parsers.DocumentBuilderFactory;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//javax.xml.parsers.DocumentBuilder;
DocumentBuilder builder=dbf.newDocumentBuilder();
doc=builder.newDocument();
if(doc!=null){
people=doc.createElement("PEOPLE");
person=doc.createElement("PERSON");
person.setAttribute("PERSONID","E01");
name=doc.createElement("NAME");
name.appendChild(doc.createTextNode("Micro.Wu"));
address=doc.createElement("ADDRESS");
address.appendChild(doc.createTextNode("25# of 17 WestChook street Singapore"));
tel=doc.createElement("TEL");
tel.appendChild(doc.createTextNode("(085)32453323"));
fax=doc.createElement("FAX");
fax.appendChild(doc.createTextNode("(085)32453323"));
email=doc.createElement("EMAIL");
email.appendChild(doc.createTextNode("[email protected]"));
people.appendChild(person);
person.appendChild(name);
person.appendChild(address);
person.appendChild(tel);
person.appendChild(fax);
person.appendChild(email);
doc.appendChild(people);
//org.apache.crimson.tree.XmlDocument;
((XmlDocument)doc).write(System.out);
//--write output xml file;
//((XmlDocument)doc).write(new java.io.FileOutputStream("out.xml"));
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
import org.jdom.input.SAXBuilder;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.JDOMException;import java.io.File;
import java.io.FileOutputStream;public class BuildReadmeXMLFile
{
/**
* 功能:构造函数
* 参数:无
* 返回值:无
*/
public BuildReadmeXMLFile()
{}
/**
* 功能:开始写Readme文件
* 参数:strType:创建文件类型,文件可能是Anytime或Daily类型;
* 返回值:无
*/
public void startBuild(String strType)
{
this.m_strCurrentType = strType;
Element elementCommon = null;
Element elementRPS = null;
if (ANYTIME == strType) {
m_elementRoot = new Element("Anytime");
elementCommon = new Element("Common");
elementRPS = new Element("RPS");
}
else if (DAILY == strType) {
m_elementRoot = new Element("Daily");
elementCommon = new Element("Common");
elementRPS = new Element("RPS");
}
m_elementRoot.addContent(elementCommon);
m_elementRoot.addContent(elementRPS);
}
/**
* 功能:完成写Readme文件
* 参数:strName:保存文件名;
* 返回值:无
*/
public boolean finishBuild(String strName)
{
//m_strCurrentType
String strSavePath = m_parse.getValue("xmlfile", "readmedir", "dir");
strSavePath += (File.separator + m_strCurrentType + "_Readme_" + strName);
System.out.println(strSavePath);
XMLOutputter xmlOutput = new XMLOutputter(" ", true);
m_document = new Document(m_elementRoot);
try {
xmlOutput.setEncoding("GBK");
FileOutputStream fileOutput = new FileOutputStream(strSavePath);
xmlOutput.output(m_document, fileOutput);
fileOutput.close();
}
catch(Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 功能:向Readme.xml文件中添加记录
* 参数:strType:类型,保留值;strValue:添加的要用到的具体值,此处为下发文件的文件名;
* 返回值:无
*/
public void addCommon(String strType, String strFileName)
{
String strSaveDir = m_parse.getValue("xmlfile", "savedir", "dir");
strSaveDir += File.separator;
strSaveDir += strFileName;
System.out.println(strSaveDir);
Element elementFile = new Element("File");
Element elementType = new Element("Type");
Element elementPath = new Element("Path");
elementFile.addContent(elementType);
elementFile.addContent(elementPath);
elementType.setText(strType);
elementPath.setText(strSaveDir);
Element elementCommon = m_elementRoot.getChild("Common");
elementCommon.addContent(elementFile);
} /**
* 功能:向Readme.xml文件中添加记录
* 参数:strDestIP:目标IP地址;strType:类型,保留值;strValue:添加的要用到的具体值;
* 返回值:无
*/
public void addRPS(String strDestIP, String strFileType, String strFileName)
{
String strSaveDir = m_parse.getValue("xmlfile", "savedir", "dir");
strSaveDir += File.separator;
strSaveDir += strFileName;
System.out.println(strSaveDir); Element elementHost = getElement(strDestIP);//new Element("Host"); Element elementFile = new Element("File");
Element elementType = new Element("Type");
Element elementPath = new Element("Path"); elementFile.addContent(elementType);
elementFile.addContent(elementPath);
elementType.setText(strFileType);
elementPath.setText(strSaveDir);
if (null != elementHost) {
Element elementRPS = m_elementRoot.getChild("RPS");
}
else {
elementHost = new Element("Host");
elementHost.setAttribute("IP", strDestIP);
Element elementRPS = m_elementRoot.getChild("RPS");
elementRPS.addContent(elementHost);
} elementHost.addContent(elementFile);
}
private Element getElement(String strAttributeValue)
{
Element elementRPS = m_elementRoot.getChild("RPS");
java.util.List hostList = elementRPS.getChildren();
int size = hostList.size();
System.out.println("abasdfka;sdfk;alsdfk");
System.out.println(strAttributeValue);
if (!hostList.isEmpty()) {
for (int i = 0; i < size; i++) {
Element element = (Element)hostList.get(i);
if (strAttributeValue.equalsIgnoreCase(element.getAttributeValue("IP"))) {
System.out.println("in");
System.out.println(element.getName());
return element;
}
}
}
System.out.println("out");
return null;
}
private void init()
{
m_strReadmeDir = m_parse.getValue("xmlfile", "readmedir", "dir");
m_strLogDir = m_parse.getValue("system", "log", "dir");
}
// 成员变量
public final static String ANYTIME = "Anytime";
public final static String DAILY = "Daily";
private static String m_strCurrentType = "Daily";
private String m_strReadmeDir = null;
private String m_strLogDir = null;
private Document m_document = null;
private Element m_elementRoot = null;
private ParseConfig m_parse = ParseConfig.getInstance("SysConfig.xml");
// 成员变量
public static void main(String[] args)
{
BuildReadmeXMLFile readmeXML = new BuildReadmeXMLFile();
readmeXML.startBuild(BuildReadmeXMLFile.ANYTIME);
readmeXML.addCommon("这也是一个测试!", "http://efg1");
readmeXML.addCommon("这也是一个测试!", "http://efg2");
readmeXML.addRPS("172.20.54.2", "这是一个测试!", "http://abc1");
readmeXML.addRPS("172.20.54.2", "这是一个测试!", "http://abc2");
readmeXML.addRPS("172.20.54.22", "这是一个测试!", "http://abc11");
readmeXML.addRPS("172.20.54.22", "这是一个测试!", "http://abc22");
readmeXML.finishBuild("xxxx22222.xml");
readmeXML.startBuild(BuildReadmeXMLFile.DAILY);
readmeXML.addCommon("这也是一个测试!", "http://efg1");
readmeXML.addCommon("这也是一个测试!", "http://efg2");
readmeXML.addRPS("172.20.54.2", "这是一个测试!", "http://abc1");
readmeXML.addRPS("172.20.54.2", "这是一个测试!", "http://abc2");
readmeXML.addRPS("172.20.54.22", "这是一个测试!", "http://abc22");
readmeXML.addRPS("172.20.54.22", "这是一个测试!", "http://abc33");
readmeXML.finishBuild("xxxx11111.xml");
System.out.print("OK");
}
}