Java operations on XML files and java operations on xml files
Zookeeper can be used in four ways: DOM, JDOM, Sax, and Dom4j.
What is the difference between Node and Element in xml?
Node is relative to the data structure like Tree, while Element is the concept in xml.
A node is not necessarily an element, but an element must be a node.
A node that must contain complete information is an element.
Element is inherited from Node, and Element can have attributes and subnodes.
Add
AppendElement ()
CreateElement ()
Modify
SetAttribute ()
SetTextContent ()
Delete
GetParentNode (). removeChild ()
Save
TransformerFactory
DOMSource: act as the holder of the conversion Source tree in the form of the Document Object Model tree
StreamResult: the owner of the Conversion Result. It can be a tag in XML, plain text, HTML, or some other formats.
Search
XPathFactory:
XPath: provides access to the XPath computing environment and expressions.
XPath. evaluate ()
How to use java to operate xml files
Dom4j is a good thing and is easy to use.
Here is a very detailed introduction. ibm's article describes in detail the steps for creating and modifying xml.
Www.ibm.com/..j.html
Operate XML files in java
Public void testWrite (){
// Create related nodes
Element root = new Element ("person ");
Element name = new Element ("name ");
Name. addContent ("James ");
Element age = new Element ("age ");
Age. addContent ("24 ");
// Add the child node to the parent node
Root. addContent (name );
Root. addContent (age );
// Create a document Object
Document doc = new Document (root );
// Output the created xml file
XMLOutputter out = new XMLOutputter ();
Try {
Out. output (doc, new FileOutputStream ("c:/person. xml "));
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}