??xml version="1.0" encoding="utf-8" standalone="yes"?>
Q一Q移除节点及属?/strong>
输出l果为: 1。正的删除了类型ؓsociety的book节点
分析Q?/strong>
W二个输出结果不能删除sex节点Q我们需要看dom4j的API 从中我们可以看出Qremove只能用在它自q直接孩子节点上,不能用在孙子节点上,因ؓsex节点不是root节点的直接孩子节点,所以不能删除;而sex节点却是author节点的直接孩子节点,所以第三个输出可以删除?/p>
Q二Q将两个Document合ƈZ个Document
先看一个错误的情况 Q?Q用add()Ҏd 调用CombineDocument函数Q会出现以下错误Q?/p>
org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultElement@17bd6a1 [Element: <author attributes: []/>]" could not be added to the element "root" because: The Node already has an existing parent of "root" xCauthor节点已经有一个root节点了,不能再添加到另一个节点上厅R?/p>
Q?Q用appendContent()Ҏ 卛_doc_book.getRootElement().add(author); 改ؓQdoc_book.getRootElement().appendContent(author); 输出l果为: <?xml version="1.0" encoding="UTF-8"?> 可以看出Q缺了author节点Q只是把author节点的子节点d上去了,但是由此可见QappendContentҎ是有希望的?/p>
我们看一下dom4j的API: Q?Q用正的appendContentҎ :Element author=(Element)doc_author.selectSingleNode("http://author"); doc_book.getRootElement().appendContent(author); 改ؓQdoc_book.getRootElement().appendContent(doc_author.getRootElement()); 输出Q?/p>
<?xml version="1.0" encoding="UTF-8"?> 是正结?/p>
Q?Q另一U可行的Ҏ
Q一Q创建Document的基本操?/strong>
/** 调用BaseOperationQ输出结果ؓQ?/p>
<?xml version="1.0" encoding="UTF-8"?>
Q二Q根据一个符合Document格式的字W串来生成一个Document
/**字W串转化为Document 调用CZQ?/p>
String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>"; Document document = parserStrtoDocument(str); 输出l果为: <?xml version="1.0" encoding="UTF-8"?>
Q三Q取得xml节点属性的基本Ҏ
/** 调用getBaseInfofromDocumentQ输出结果ؓQ?/p>
书名QJava
Q四Q利用P代,xpath取得节点及其属性?/strong>
/**利用q代Qxpath取得xml的节点及其属性?br /> * @throws DocumentException
//查找作者姓?br /> Element author=(Element)document.selectSingleNode("http://author"); 调用getComplexInfofromDocumentQ输出结果ؓQ?/p>
<book type="society"><Name>Society security</Name><price>130</price></book>
备注Q调用该Ҏ之前Q应该先向工E中d支持xpath的jar包,否则Q会出现以下错误Q?/strong>
java.lang.NoClassDefFoundError: org/jaxen/JaxenException 只需要引入jaxen包就行了Q我使用的是hibernate包中的jaxen-1.1-beta-7.jar包?/p>
]]>
/** */
/**
U除节点和属性的操作
*
@throws
DocumentException
*/
public
void
RemoveOperator()
throws
DocumentException
...
{
//
待生成xml的字W串
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>
"
;
//
生成一个Document
Document document
=
DocumentHelper.parseText(str);
Element root
=
document.getRootElement();
//
删除cd为society的book节点
Element book_society
=
(Element)document.selectSingleNode(
"
//book[@type='society']
"
);
root.remove(book_society);
System.out.println(
"
1。正的删除了类型ؓsociety的book节点
"
);
System.out.println(document.asXML());
//
删除sex节点
Element sex
=
(Element)root.selectSingleNode(
"
//sex
"
);
//
从root节点删除
root.remove(sex);
System.out.println(
"
2。这h不能删除sex节点?/span>
"
);
System.out.println(document.asXML());
//
从author节点删除
root.element(
"
author
"
).remove(sex);
System.out.println(
"
3。这样就可以正确删除sex节点
"
);
System.out.println(document.asXML());
//
删除属?/span>
Attribute type
=
root.element(
"
book
"
).attribute(
"
type
"
);
root.element(
"
book
"
).remove(type);
System.out.println(
"
4。正删除book节点的type属?/span>
"
);
System.out.println(document.asXML());
}
<?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。这h不能删除sex节点?br /><?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。这样就可以正确删除sex节点
<?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。正删除book节点的type属?br /><?xml version="1.0" encoding="UTF-8"?>
<root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>remove
public boolean remove(Element element)
Element
if the node is an immediate child of this branch. If the given node is not an immediate child of this branch then the Node.detach()
method should be used instead.
element
- is the element to be removed
public
void
CombineDocument()
throws
DocumentException
...
{
//
待生成两个Document的字W串
String str_book
=
"
<root><book type='science'><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>
"
;
//
生成两个Document
Document doc_book
=
DocumentHelper.parseText(str_book);
Document doc_author
=
DocumentHelper.parseText(str_author);
//
取出doc_author的author节点Q添加到doc_book的根l点
Element author
=
(Element)doc_author.selectSingleNode(
"
//author
"
);
doc_book.getRootElement().add(author);
System.out.println(doc_book.asXML());
}
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"
<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>appendContent
public void appendContent(Branch branch)
Collection.addAll(java.util.Collection)
method.
branch
- is the branch whose content will be added to me.
<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>
public
void
CombineDocument()
throws
DocumentException
...
{
//
待生成两个Document的字W串
String str_book
=
"
<root><book type='science'><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>
"
;
//
生成两个Document
Document doc_book
=
DocumentHelper.parseText(str_book);
Document doc_author
=
DocumentHelper.parseText(str_author);
//
新生成一个Document
Element author
=
DocumentHelper.createElement(
"
author
"
);
author.appendContent((Element)doc_author.selectSingleNode(
"
//author
"
));
//
当前author无父节点,所以可以用addҎd
doc_book.getRootElement().add(author);
System.out.println(doc_book.asXML());
}
]]>
* xml基本操作
*/
public void BaseOperation(){
//创徏一个document
Document document=DocumentHelper.createDocument();
//创徏根结?br /> Element root=document.addElement("root");
//为根l点d一个book节点
Element book1=root.addElement("book");
//为book1d属性type
book1.addAttribute("type","science");
//为book1dname子节?br /> Element name1=book1.addElement("Name");
//q设|其name?Java"
name1.setText("Java");
//为book1创徏一个price节点,q设其hgؓ100
book1.addElement("price").setText("100");
//为根l点dW二个book节点Qƈ讄该book节点的type属?br /> Element book2=root.addElement("book").addAttribute("type","science");
//为book1dname子节?br /> Element name2=book2.addElement("Name");
//q设|其name?Oracle"
name2.setText("Oracle");
//为book1创徏一个price节点,q设其hgؓ200
book2.addElement("price").setText("200");
//输出xml
System.out.println(document.asXML());
}
<root>
<book type="science">
<Name>Java</Name>
<price>100</price>
</book>
<book type="science">
<Name>Oracle</Name>
<price>200</price>
</book>
</root>
* @param str 输入的字W串
* @return 生成的document
* @throws DocumentException
*/
public Document parserStrtoDocument(String str) throws DocumentException{
Document document=DocumentHelper.parseText(str);
return document;
}
System.out.println(document.asXML());
<root>
<book type="science">
<Name>Java</Name>
<price>100</price>
</book>
</root>
* 取得xml的节点和属性的?br /> * @throws DocumentException
*/
public void getBaseInfofromDocument() throws DocumentException{
String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";
//生成一个Document
Document document = DocumentHelper.parseText(str);
//取得根结?br /> Element root=document.getRootElement();
//取得book节点
Element book=root.element("book");
//取得book节点的type属性的?br /> String type=book.attributeValue("type");
//取得Name节点
Element name=book.element("Name");
//取得书名
String bookname=name.getText();
//取得书的价钱
int price=Integer.parseInt(book.element("price").getText());
//输出书目信息
System.out.println("书名Q?+bookname);
System.out.println("所属类别:"+type);
System.out.println("hQ?+price);
}
所属类别:science
hQ?00
*/
public void getComplexInfofromDocument() throws DocumentException{
String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"
+"<book type='science'><Name>Oracle</Name><price>120</price></book>"
+"<book type='society'><Name>Society security</Name><price>130</price></book>"
+"<author><name>chb</name></author></root>";
//生成一个Document
Document document = DocumentHelper.parseText(str);
//提取cd?society"的书
//此处需要添加支持xpath的jar包,详细见备?br /> Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']");
System.out.println(society_book.asXML());
//提取h节点的列?br /> System.out.println("-----------h列表-------------");
List price=document.selectNodes("http://price");
for(int i=0;i<price.size();i++){
Element elem_price=(Element)price.get(i);
System.out.println(elem_price.getText());
}
//循环根结点下的所有节点,若当前节点ؓbookQ则输出q本书的详细信息
System.out.println("-------------书目详情------------");
System.out.println("书名\t\tcd\t\th");
Element root=document.getRootElement();
Iterator iterator=root.elementIterator();
while(iterator.hasNext()){
Element element=(Element)iterator.next();
if(element.getName().equals("book")){
System.out.print(element.element("Name").getText()+"\t");
System.out.print(element.attributeValue("type")+"\t\t");
System.out.print(element.element("price").getText()+"\n");
}
}
System.out.println("---------"+author.element("name").getText()+"----------");
//提取作者的所有书目名U?br /> Iterator iterator_book=root.elementIterator("book");
while(iterator_book.hasNext()){
Element book=(Element)iterator_book.next();
System.out.print(book.element("Name").getText()+"\t");
}
//属性P?br /> System.out.println("\n-------属性P?-------");
String str1="<book type='science' name='Java' price='100'/>";
Document document1=DocumentHelper.parseText(str1);
//开始P?br /> Iterator iterator_attribute=document1.getRootElement().attributeIterator();
while(iterator_attribute.hasNext()){
//提取当前属?br /> Attribute attribute=(Attribute)iterator_attribute.next();
System.out.println(attribute.getName()+":"+attribute.getValue());
}
}
-----------h列表-------------
100
120
130
-------------书目详情------------
书名 cd h
Java science 100
Oracle science 120
Society security society 130
---------chb----------
Java Oracle Society security
-------属性P?-------
type:science
name:Java
price:100
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82)
at xml_chb.dom4j_chb.main(dom4j_chb.java:92)
Exception in thread "main"
Hashtable<String, String> hash=new Hashtable<String, String>();
for(int i=0;i<str.length;i++){
if(!hash.containsKey(str[i]))
hash.put(str[i], str[i]);
}
Enumeration enumeration=hash.keys();
String[] str_new=new String[hash.size()];
int i=0;
while(enumeration.hasMoreElements()){
str_new[i]=enumeration.nextElement().toString();
i++;
}
return str_new;
}
CZQ?br /> String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
mobile=checkArray(mobile);
for(int i=0;i<mobile.length;i++)
System.out.println(mobile[i]);
输出l果为:
13811071503
13811071501
13811071500
2.A,B均ؓ字符串数l,扑և在A中存在,而在B中不存在的字W串
public String[] compareArray(String[] A,String[] B){
Hashtable<String, String> hash=new Hashtable<String, String>();
Hashtable<String, String> hash_new=new Hashtable<String, String>();
for(int i=0;i<B.length;i++)
hash.put(B[i], B[i]);
for(int i=0;i<A.length;i++){
if(!hash.containsKey(A[i]))
hash_new.put(A[i], A[i]);
}
String[] C=new String[hash_new.size()];
int i=0;
Enumeration enumeration=hash_new.keys();
while(enumeration.hasMoreElements()){
C[i]=enumeration.nextElement().toString();
i++;
}
return C;
}
CZQ?br /> String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
String[] mobile3=compareArray(mobile1,mobile2);
for(int i=0;i<mobile3.length;i++)
System.out.println(mobile[i]);
输出l果Q?br /> 13811071503
13811071501
存在的问题:
每次都是倒序Q可以再对程序稍加改动,变成正序?/font>
]]>
1。实例化对象Q可以用如下两种ҎQ?br />DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
因ؓDecimalFormatl承自NumberFormat?br />2。设定小C?br />pȝ默认数位数?Q如Q?br /> DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.println(df.format(12.3456789));
输出Q?2.346
现在可以通过如下Ҏ把小Cؓ设ؓ两位Q?br /> df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789));
则输ZؓQ?2.35
3。将数字转化为百分比输出Q有如下两种ҎQ?br />(1)
df.applyPattern("##.##%");
System.out.println(df.format(12.3456789));
System.out.println(df.format(1));
System.out.println(df.format(0.015));
输出分别为:1234.57% 100% 1.5%
(2)
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789*100)+"%");
System.out.println(df.format(1*100)+"%");
System.out.println(df.format(0.015*100)+"%");
输出分别为:
1,234.57% 100% 1.5%
4。设|分l大?br /> DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
System.out.println(df1.format(123456789));
输出Q?,23,45,67,89
q可以通过df1.setGroupingUsed(false);来禁用分l设|,如:
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
df1.setGroupingUsed(false);
System.out.println(df1.format(123456789));
输出Q?23456789
5。设|小Cؓ必须??br /> DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();
df2.applyPattern("0.00");
System.out.println(df2.format(1.2));
输出Q?.20