当前位置: 技术问答>java相关
求例子,用JDOM处理XML文档
来源: 互联网 发布时间:2015-08-03
本文导语: 有一些方法, 比如给参数:elemnetname,attributename 返回:attributevlaue 再比如 给参数:elementname,attributename,attributevalue 处理结果:在elementname中加入属性:attributename="attributevalue" 请大虾给几个例子,谢谢! ...
有一些方法,
比如给参数:elemnetname,attributename
返回:attributevlaue
再比如
给参数:elementname,attributename,attributevalue
处理结果:在elementname中加入属性:attributename="attributevalue"
请大虾给几个例子,谢谢!
比如给参数:elemnetname,attributename
返回:attributevlaue
再比如
给参数:elementname,attributename,attributevalue
处理结果:在elementname中加入属性:attributename="attributevalue"
请大虾给几个例子,谢谢!
|
//use jdom for xml parsing
/*
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
public class ConfigParser {
org.jdom.Document document;
org.jdom.Element rootElement;
static String configFile= "Ico-opData/ico-opCRMconfig.xml";
public ConfigParser(){
try{
DOMBuilder builder = new DOMBuilder();
document = builder.build(getW3CDocument());
rootElement = document.getRootElement();
//}catch(JDOMException e) {
// e.printStackTrace();
}catch(NullPointerException e) {
e.printStackTrace();
}catch(Exception e){
}
}
public org.w3c.dom.Document getW3CDocument(){
org.w3c.dom.Document myDoc;
File inputfile = new File(configFile);
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
myDoc = db.parse(inputfile);
return myDoc;
}catch(FileNotFoundException fe){
System.out.println("File no found: " + fe.getMessage());
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}catch(ParserConfigurationException pe){
System.err.println(pe.getMessage());
}catch(SAXException se){
System.err.println(se.getMessage());
}catch(Exception e){
}
return null;
}
public Object[][] getAllSystemConfiguration(){
List childList = rootElement.getChildren();
Object[][] configData = new Object[childList.size()][4];
Iterator it = childList.iterator();
int i = 0;
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
String parameterValue = currentElement.getChild("ParameterValue").getText().trim();
String parameterDes = currentElement.getChild("Description").getText().trim();
Boolean passwordDisplay = new Boolean(currentElement.getChild("PasswordDisplay").getText().trim());
configData[i][0] = parameterName;
configData[i][1] = parameterValue;
configData[i][2] = parameterDes;
configData[i][3] = passwordDisplay;
i = i +1;
}
return configData;
}
public String getParameterValue(String queryParameterName){
List childList = rootElement.getChildren();
Iterator it = childList.iterator();
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
if(parameterName.equals(queryParameterName)){
return currentElement.getChild("ParameterValue").getText().trim();
}
}
return null;
}
public String getParameterDescription(String queryParameterName){
List childList = rootElement.getChildren();
Iterator it = childList.iterator();
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
if(parameterName.equals(queryParameterName)){
return currentElement.getChild("Description").getText().trim();
}
}
return null;
}
public boolean setParameterDescription(String queryParameterName,
String parameterValue){return true;}
public boolean setAllParameters(String[][] parameters) throws IOException{
List childList = rootElement.getChildren();
for(int i =0; i
/*
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
public class ConfigParser {
org.jdom.Document document;
org.jdom.Element rootElement;
static String configFile= "Ico-opData/ico-opCRMconfig.xml";
public ConfigParser(){
try{
DOMBuilder builder = new DOMBuilder();
document = builder.build(getW3CDocument());
rootElement = document.getRootElement();
//}catch(JDOMException e) {
// e.printStackTrace();
}catch(NullPointerException e) {
e.printStackTrace();
}catch(Exception e){
}
}
public org.w3c.dom.Document getW3CDocument(){
org.w3c.dom.Document myDoc;
File inputfile = new File(configFile);
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
myDoc = db.parse(inputfile);
return myDoc;
}catch(FileNotFoundException fe){
System.out.println("File no found: " + fe.getMessage());
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}catch(ParserConfigurationException pe){
System.err.println(pe.getMessage());
}catch(SAXException se){
System.err.println(se.getMessage());
}catch(Exception e){
}
return null;
}
public Object[][] getAllSystemConfiguration(){
List childList = rootElement.getChildren();
Object[][] configData = new Object[childList.size()][4];
Iterator it = childList.iterator();
int i = 0;
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
String parameterValue = currentElement.getChild("ParameterValue").getText().trim();
String parameterDes = currentElement.getChild("Description").getText().trim();
Boolean passwordDisplay = new Boolean(currentElement.getChild("PasswordDisplay").getText().trim());
configData[i][0] = parameterName;
configData[i][1] = parameterValue;
configData[i][2] = parameterDes;
configData[i][3] = passwordDisplay;
i = i +1;
}
return configData;
}
public String getParameterValue(String queryParameterName){
List childList = rootElement.getChildren();
Iterator it = childList.iterator();
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
if(parameterName.equals(queryParameterName)){
return currentElement.getChild("ParameterValue").getText().trim();
}
}
return null;
}
public String getParameterDescription(String queryParameterName){
List childList = rootElement.getChildren();
Iterator it = childList.iterator();
while(it.hasNext()){
Element currentElement = (Element)it.next();
List parameterChildList = currentElement.getChildren();
Iterator pit = parameterChildList.iterator();
String parameterName = currentElement.getChild("ParameterName").getText().trim();
if(parameterName.equals(queryParameterName)){
return currentElement.getChild("Description").getText().trim();
}
}
return null;
}
public boolean setParameterDescription(String queryParameterName,
String parameterValue){return true;}
public boolean setAllParameters(String[][] parameters) throws IOException{
List childList = rootElement.getChildren();
for(int i =0; i