Document directory
Dom4j learning Summary (2)
(1) Remove nodes and attributes
/*** // ** Delete nodes and attributes
* @ Throws define entexception
*/
Public void removeoperator () throws into entexception ...{
// String of the XML to be generated
String STR = "<root> <book type = 'science '> <Name> JAVA </Name> <price> 100 </price> </book>"
+ "<Book type = 'society'> <Name> Society Security </Name> <price> 130 </price> </book>"
+ "<Author> <Name> CHB </Name> <sex> boy </sex> </author> </root> ";
// Generate a document
Document document = incluenthelper. parsetext (STR );
Element root = Document. getrootelement ();
// Delete the book node of the society type
Element book_society = (element) document. selectsinglenode ("// book [@ type = 'society']");
Root. Remove (book_society );
System. Out. println ("1. The book node of the society type is deleted correctly ");
System. Out. println (document. asxml ());
// Delete the sex Node
Element sex = (element) root. selectsinglenode ("// sex ");
// Delete from the root node
Root. Remove (sex );
System. Out. println ("2. In this way, the sex node cannot be deleted ");
System. Out. println (document. asxml ());
// Delete from the author Node
Root. element ("author"). Remove (sex );
System. Out. println ("3. In this way, you can correctly Delete the sex node ");
System. Out. println (document. asxml ());
// Delete attributes
Attribute type = root. element ("book"). Attribute ("type ");
Root. element ("book"). Remove (type );
System. Out. println ("4. Delete the type attribute of the book node correctly ");
System. Out. println (document. asxml ());
}
Output result:
1. The Society book node is deleted correctly.
<? XML version = "1.0" encoding = "UTF-8"?>
<Root> <book type = "science"> <Name> JAVA </Name> <price> 100 </price> </book> <author> <Name> CHB </ name> <sex> boy </sex> </author> </root>
2. In this way, the sex node cannot be deleted.
<? XML version = "1.0" encoding = "UTF-8"?>
<Root> <book type = "science"> <Name> JAVA </Name> <price> 100 </price> </book> <author> <Name> CHB </ name> <sex> boy </sex> </author> </root>
3. In this way, the sex node can be deleted correctly.
<? XML version = "1.0" encoding = "UTF-8"?>
<Root> <book type = "science"> <Name> JAVA </Name> <price> 100 </price> </book> <author> <Name> CHB </ name> </author> </root>
4. Delete the type attribute of the book node correctly
<? XML version = "1.0" encoding = "UTF-8"?>
<Root> <book> <Name> JAVA </Name> <price> 100 </price> </book> <author> <Name> CHB </Name> </author> </root>
Analysis:
The second output result cannot delete the sex node. We need to check the dom4j API
Remove
public boolean remove(Element element)
-
Removes the given
Element
If the node is an immediate child of this branch. If the given node is not an immediate child of this branch then
Node.detach()
Method shoshould be used instead.
-
-
-
Parameters:
-
element
-Is the element to be removed
-
Returns:
-
True if the element was removed
As we can see, remove can only be used on its own direct child node, not on its grandson node, because the sex node is not a direct child node of the root node, so it cannot be deleted; the sex node is the direct child node of the author node, so the third output can be deleted.
(2) merge two documents into one document
Let's first look at an error.
(1) Use the add () method to add
Public void combinedocument () throws into entexception ...{
// String of the two documents to be generated
String str_book = "<root> <book type = 'scientific '> <Name> JAVA </Name> <price> 100 </price> </book>"
+ "<Book type = 'society'> <Name> Society Security </Name> <price> 130 </price> </book>"
+ "</Root> ";
String str_author = "<root> <author> <Name> CHB </Name> <sex> boy </sex> </author> </root> ";
// Generate two documents
Document doc_book = incluenthelper. parsetext (str_book );
Document doc_author = incluenthelper. parsetext (str_author );
// Retrieve the doc_author node and add it to the doc_book Root Node
Element author = (element) doc_author.selectsinglenode ("// Author ");
Doc_book.getrootelement (). Add (author );
System. Out. println (doc_book.asxml ());
}
When you call the combinedocument function, the following error occurs:
Org. dom4j. illegaladdexception: The Node "org. dom4j. tree. defaultelement @ 17bd6a1 [element: <author attributes: []/>] "cocould not be added to the element" root "because: the node already has an existing parent of" root"
At org. dom4j. Tree. abstractelement. addnode (abstractelement. Java: 1521)
At org. dom4j. Tree. abstractelement. Add (abstractelement. Java: 1002)
At xml_chb.dom4j_chb.combinedocument (dom4j_chb.java: 189)
At xml_chb.dom4j_chb.main (dom4j_chb.java: 199)
Exception in thread "Main"
It indicates that the author node already has a root node and cannot be added to another node.
(2) Use the appendcontent () method
Doc_book.getrootelement (). Add (author );
Changed to doc_book.getrootelement (). appendcontent (author );
Output result:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Book type = "science"> <Name> JAVA </Name> <price> 100 </price> </book>
<Book type = "Society"> <Name> Society Security </Name> <price> 130 </price> </book>
<Name> CHB </Name> <sex> boy </sex>
</Root>
We can see that the author node is missing, but the child node of the author node is added. However, the appendcontent method is promising.
Let's take a look at the dom4j API:
Appendcontent
public void appendContent(Branch branch)
-
Appends the content of the given branch to this branch instance. This method behaves like
Collection.addAll(java.util.Collection)
Method.
-
-
-
Parameters:
-
branch
-Is the branch whose content will be added to me.
(3) Use the correct appendcontent Method
Set: element author = (element) doc_author.selectsinglenode ("// Author ");
Doc_book.getrootelement (). appendcontent (author );
Change to: doc_book.getrootelement (). appendcontent (doc_author.getrootelement ());
Output:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Book type = "science"> <Name> JAVA </Name> <price> 100 </price> </book>
<Book type = "Society"> <Name> Society Security </Name> <price> 130 </price> </book>
<Author> <Name> CHB </Name> <sex> boy </sex> </author>
</Root>
Is the correct result
(4) Another Feasible Method
Public void combinedocument () throws into entexception ...{
// String of the two documents to be generated
String str_book = "<root> <book type = 'scientific '> <Name> JAVA </Name> <price> 100 </price> </book>"
+ "<Book type = 'society'> <Name> Society Security </Name> <price> 130 </price> </book>"
+ "</Root> ";
String str_author = "<root> <author> <Name> CHB </Name> <sex> boy </sex> </author> </root> ";
// Generate two documents
Document doc_book = incluenthelper. parsetext (str_book );
Document doc_author = incluenthelper. parsetext (str_author );
// Generate a new document
Element author = incluenthelper. createelement ("author ");
Author. appendcontent (element) doc_author.selectsinglenode ("// Author "));
// The current author has no parent node, so you can use the add method to add
Doc_book.getrootelement (). Add (author );
System. Out. println (doc_book.asxml ());
}