Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.ipaas.ideploy.plugin.util; import java.io.IOException; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; /** * * ??? * * @author Chenql */ @SuppressWarnings({ "unchecked", "rawtypes" }) public class XmlUtil { public static String ASSEMBLY_ELEMENT = "build.plugins.plugin.configuration.descriptors.descriptor"; public static String BUILD_NAME_ELEMENT = "build.finalName"; public static String ARTIFACTID_ELEMENT = "artifactId"; public static String VERSION_ELEMENT = "version"; public static String ICE_CONTAINER = "ice"; /** * ? * * @param XML? * @param elementNames * ?? . * @param dufaultValue * ? * @return */ public static String getString(String fileName, String elementNames, String defaultValue) { XMLConfiguration config; try { config = new XMLConfiguration(fileName); } catch (ConfigurationException e) { ConsoleHandler.error("??[" + fileName + "]:" + e.getMessage()); return null; } // ?????? String str = config.getString(elementNames, defaultValue); return str; } public static String getString(String fileName, String elementNames) { XMLConfiguration config; try { config = new XMLConfiguration(fileName); } catch (ConfigurationException e) { ConsoleHandler.error("??[" + fileName + "]:" + e.getMessage()); return null; } // ?????? String str = config.getString(elementNames); return str; } public static void main(String[] args) throws IOException { System.out.println(XmlUtil.getString( "D:/Users/TY-Chenql/runtime-EclipseApplication/crs_mave_ice/script/assembly.xml", "id", "sc")); try { XMLConfiguration config = new XMLConfiguration("D:/Users/TY-Chenql/workspace/crs_mave_ice/pom.xml"); // ?????? String str = config.getString("build.plugins.plugin.configuration.descriptors.descriptor"); // System.out.println(str); // // ?.???? // List<Object> names = config.getList("student.name"); // System.out.println(Arrays.toString(names.toArray())); // // ???a,b,c,d ??? // List<Object> titles = config.getList("title"); // System.out.println(Arrays.toString(titles.toArray())); // // ? ??[@??] ?? // String size = config.getString("ball[@size]"); // System.out.println(size); // // ???? ??(??) ?? // String id = config.getString("student(1)[@id]"); // System.out.println(id); // // String go = config.getString("student.name(0)[@go]"); // System.out.println(go); // /** // * ? tom [lily, lucy] [abc, cbc, bbc, bbs] 20 2 common1 // * // */ } catch (ConfigurationException e) { e.printStackTrace(); } } }