dom4j

create XML:

public void generateDocument() {

Document document = DocumentHelper.createDocument();

Element catalogElement = document.addElement("catalog");

catalogElement.addComment("An XML Catalog");

catalogElement.addProcessingInstruction("target", "text");

Element journalElement = catalogElement.addElement("journal");

journalElement.addAttribute("title", "XML Zone");

journalElement.addAttribute("publisher", "IBM developerWorks");

Element articleElement = journalElement.addElement("article");

articleElement.addAttribute("level", "Intermediate");

articleElement.addAttribute("date", "December-2001");

Element titleElement = articleElement.addElement("title");

titleElement.setText("Java configuration with XML Schema");

Element authorElement = articleElement.addElement("author");

Element firstNameElement = authorElement.addElement("firstname");

firstNameElement.setText("Marcello");

Element lastNameElement = authorElement.addElement("lastname");

lastNameElement.setText("Vitaletti");

document.addDocType("catalog", null, "file:///home/juyon/catalog.dtd");

try {

XMLWriter output = new XMLWriter(new FileWriter(new File(

"/home/juyon/catalog.xml")));

output.write(document);

output.close();

} catch (IOException e) {

System.out.println(e.getMessage());

}

}


modify XML:

 

public void modifyDocument(File inputXml) {

try {

SAXReader saxReader = new SAXReader();

saxReader.setValidation(false);

saxReader.setEntityResolver(new EntityResolver() {

@Override

public InputSource resolveEntity(String publicId,

String systemId) throws SAXException, IOException {

return new InputSource(new StringBufferInputStream(""));

}

});

Document document = saxReader.read(inputXml);

List list = document.selectNodes("//article/@level");

Iterator iter = list.iterator();

while (iter.hasNext()) {

Attribute attribute = (Attribute) iter.next();

if (attribute.getValue().equals("Intermediate"))

attribute.setValue("Introductory");

}

 

list = document.selectNodes("//article/@date");

iter = list.iterator();

while (iter.hasNext()) {

Attribute attribute = (Attribute) iter.next();

if (attribute.getValue().equals("December-2001"))

attribute.setValue("October-2002");

}

list = document.selectNodes("//article");

iter = list.iterator();

while (iter.hasNext()) {

Element element = (Element) iter.next();

Iterator iterator = element.elementIterator("title");

while (iterator.hasNext()) {

Element titleElement = (Element) iterator.next();

if (titleElement.getText().equals(

"Java configuration with XML Schema"))

titleElement

.setText("Create flexible and extensible XML schema");

}

}

list = document.selectNodes("//article/author");

iter = list.iterator();

while (iter.hasNext()) {

Element element = (Element) iter.next();

Iterator iterator = element.elementIterator("firstname");

while (iterator.hasNext()) {

Element firstNameElement = (Element) iterator.next();

if (firstNameElement.getText().equals("Marcello"))

firstNameElement.setText("Ayesha");

}

}

list = document.selectNodes("//article/author");

iter = list.iterator();

while (iter.hasNext()) {

Element element = (Element) iter.next();

Iterator iterator = element.elementIterator("lastname");

while (iterator.hasNext()) {

Element lastNameElement = (Element) iterator.next();

if (lastNameElement.getText().equals("Vitaletti"))

lastNameElement.setText("Malik");

}

}

XMLWriter output = new XMLWriter(new FileWriter(new File(

"/home/juyon/catalog-modified.xml")));

output.write(document);

output.close();

}

 

catch (DocumentException e) {

System.out.println(e.getMessage());

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值