導航:首頁 > 編程語言 > javaxml添加節點

javaxml添加節點

發布時間:2023-05-15 11:53:47

java中的XML追加內容,追加節點,和節點的內容,還要常用的設計模式的代碼

/**
* 根據Xml文件生成Document對象
*
* @param file
* xml文件路徑
* @return Document對象
* @throws DocumentException
*/
public static Document getDocument(String path) throws DocumentException {
File file = new File(path);
SAXReader xmlReader = new SAXReader();
return xmlReader.read(file);
}

/**
* 根據輸入流生成Document對象
*
* @param is
* 輸入流
* @return Document對象
* @throws DocumentException
*/
public static Document getDocument(InputStream is) throws DocumentException {
SAXReader xmlReader = new SAXReader();
return xmlReader.read(is);
}

/**
* 根據Document得到根結點
*
* @param doc
* Document目錄
* @return 根結點
* @throws DocumentException
*/
public static Element getRoot(String path) throws DocumentException {
Document doc = getDocument(path);
return doc.getRootElement();
}

/**
* 取出當前結點下的所有子結點
*
* @param root
* 當前結點
* @return 一組Element
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Element> getElements(String path)
throws DocumentException {
Element root = getRoot(path);
return root.elements();
}

/**
* 根據元素名稱返回一組Element
*
* @param root
* 當前結點
* @param name
* 要返回的元素名稱
* @return 一組Element
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Element> getElementsByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.elements(name);
}

/**
* 根據元素名稱返回一個元素(如果有多個元素的話,只返回第一個)
*
* @param root
* 當前結點
* @param name
* 要返回的元素名稱
* @return 一個Element元素
* @throws DocumentException
*/
public static Element getElementByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.element(name);
}

/**
* 根據當前元素,返回該元素的所有屬性
*
* @param root
* 當前結點
* @return 當前結點的所有屬性
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
public static List<Attribute> getAttributes(String path)
throws DocumentException {
Element root = getRoot(path);
return root.attributes();
}

/**
* 根據屬性名稱,返回當前元素的某個屬性
*
* @param root
* 當前結點
* @return 當前結點的一個屬性
* @throws DocumentException
*/
public static Attribute getAttributeByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.attribute(name);
}
public static List<Element> getElementWithAttribute(String path,String attr,String value) throws DocumentException{
List<Element> elist = new ArrayList<Element>() ;
List<Element> list = getElements(path);
for(Element element:list){
if(element.attribute(attr).getText().equals(value)){
elist.add(element);
}
}
try {
elist.add(getElementSelf(path, attr, value));
} catch (Exception e) {
// TODO: handle exception
}
return elist;
}
public static Element getElementSelf(String path,String attr,String value) throws DocumentException{
Element element = null ;
List<Element> list = getElements(path);
for(Element e:list){
if(e.attribute(attr).getText().equals(value)){
element = e ;
}
}
return element;
}

㈡ java如何在為XML添加新節點

dom操作Java,需要下載dom4j.jar包。
具體操作請見:http://blog.csdn.net/cds27/archive/2008/03/02/2139110.aspx

㈢ java 添加節點並保存成XML問題

在java編程中,用dom4j的api來處理xml,很簡單的,給段代碼,需要導入dom4j.jar

Documentdoc=DocumentHelper.createDocument();

//根節點

ElementrootEle=doc.addElement("root");

Elementele1=rootEle.addElement("ele1");

ele1.addText("節點1");

Elementele2=rootEle.addElement("ele2");

ele2.addText("節點2");

System.out.println(doc.asXML());

㈣ java對xml文件添加節點

/*
這里使用了dom4j組件,你需要自己去下載dom4j。
其中a.xml是你的源文件。
這個程序沒有向磁碟中創建一個新文件,你可以自己修改代碼。
例如:
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
可以修改為
XMLWriter xmlWriter = new XMLWriter(new FileWriter("a.xml"), format);
這樣就可以修改源文件a.xml了。
*/

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;

import java.io.*;

public class Test {
public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(new File("a.xml"));
doc.getRootElement().addElement("PARAM")
.addElement("TASKLIST").addElement("TASK")
.addElement("DATA_TRAN_ID").addCDATA("14595");

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
xmlWriter.write(doc);
xmlWriter.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}

㈤ 通過java如何操作xml向其節點中添加內容比如<say>想添加的內容</say>

<say>我是個字元串可以隨便填</say>
你是要代碼嗎
InputStream is= this.getClass.getInputStream("my.xml");
在通過 is獲得內容我就說他是 String xml=is....
String str="想添加的內容";
String newxml=xml.replace("我是個字元串可以隨便填",str);

㈥ java用dom更新xml的問題,怎麼在子節點下添加節點

用dom找到子節點,用create方法創建節點,然後把它加到子節點上

㈦ Java xml DOM(刪除,增加節點)

用jdom解析方式嘛

import java.io.*;

import org.jdom.*;

import org.jdom.output.*;

public class WriteXML

{

public void BuildXML() throws Exception

{

Element root,student,number,name,age;

root = new Element("悄指薯student-info"); //生成根元素:student-info

student = new Element("student"); //生成元素:student,該元素中將包含元素number,name,age

number = new Element("number");

name = new Element("name");

age = new Element("age");

Document doc = new Document(root); //將根元素植入文檔doc中

number.setText("001");

name.setText("lnman");

age.setText("24"啟者);

student.addContent(number);

student.addContent(name);

student.addContent(age);

root.addContent(student);

Format format = Format.getCompactFormat();

format.setEncoding("gb2312"); //設置xml文件的字元為gb2312

format.setIndent(" "); //設置xml文件的縮進為4個空格

XMLOutputter XMLOut = new XMLOutputter(format);//在元素後換行,每一層元素縮排四格

XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));

}

public static void main(String[] args) throws Exception

{

WriteXML w = new WriteXML();

System.out.println("Now we build an XML document .....");

w.BuildXML();

System.out.println("finished!");

}
}

Dom的話比較麻煩.大逗陸體上是這樣的
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
DocumentBuilder build = bf.newDocumentBuilder();
Document doc = build.parse("MyXml.xml");
NodeList nl = doc.getElementsByTagName("Structure");
Node nod = nl.item(0);
Element el = doc.createElement("Message_Info");
el.setAttribute("Name", "李四");
Element elMemo = doc.createElement("Memo");
elMemo.setTextContent("我還好");
el.appendChild(elMemo);

nod.appendChild(el);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
DOMSource sourse = new DOMSource(doc);
StreamResult result = new StreamResult("MyXml.xml");
tf.transform(sourse, result);

㈧ java 獲取xml 父節點 並追加

你用什麼解釋xml啊,
子結點。append屬性,然後父節點。append子結點啊。

㈨ 在java中,使用JDOM怎麼給XML添加一個元素節點和一個屬性節點

Element e = new Element("root");//根節點
Element ele = new Element("Class");//Class節點
ele.setAttribute("name","二年1班");//為class節點增加屬性為name,值為二年一班的節點

㈩ java生成xml後毫無格式 求高手指點 代碼如下 // 創建根節點

你應該用的jar是jdom吧.一下是格式化方法罩帶.
XMLOutputter XMLOut = new XMLOutputter(FormatXML());

public Format FormatXML(){
//格式化生成的xml文件物鉛蘆,如果不進行格式化的話,生激腔成的xml文件將會是很長的一行...
Format format = Format.getCompactFormat();
format.setEncoding("utf-8");
format.setIndent(" ");
return format;
}

閱讀全文

與javaxml添加節點相關的資料

熱點內容
風翼app為什麼進不去了 瀏覽:774
im4java壓縮圖片 瀏覽:358
數據查詢網站源碼 瀏覽:146
伊克塞爾文檔怎麼進行加密 瀏覽:886
app轉賬是什麼 瀏覽:159
php的基本語法 瀏覽:792
對外漢語pdf 瀏覽:516
如何用mamp本地web伺服器 瀏覽:869
如何加密自己js代碼 瀏覽:627
排列組合a與c的演算法 瀏覽:534
如何在文件夾中找到同名內容 瀏覽:786
有什麼app文字轉韓文配音 瀏覽:372
循環宏1命令 瀏覽:35
斐波那契數列矩陣演算法 瀏覽:674
公式保護後加密不了 瀏覽:82
java跳轉到jsp 瀏覽:819
327平方根演算法 瀏覽:216
win7美化命令行終端 瀏覽:797
免加密狗圖片 瀏覽:485
一隻透明的鳥是什麼app 瀏覽:817