本帖最后由 figolily 于 2010-03-10 06:10:00 编辑

解决方案 »

  1.   

    找一找专门对xml的函数吧,记得貌似能生成一个xml的对象,然后进程操作的。
    下面的文章你看看,也许对你有启发http://www.360doc.com/content/08/1230/09/61497_2225163.shtml
      

  2.   

    我现在已经创建出DOCUMENT了,请问用哪个函数可以直接访问到xml里的ELEMENT "city", 我不知道getElementById(idname)里面的输入IDNAME指的是什么
    -<xml_api_reply version="1">
       −<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
           −<forecast_information>
               <city data="Kuala Lumpur, Selangor"/>
               <postal_code data="Kuala Lumpur"/>
           </forecast_information>
        </weather>
    </xml_api_reply>
      

  3.   

    我用的是JDom。给你一部份源码吧。public class SteelXmlUtil {
    private final String xmlFile;
    private Element root;
    private Document document;
    private String charSet = "UTF-8"; /**
     * 构造方法
     * @param xmlFile 文件名
     */
    public SteelXmlUtil(String xmlFile) {
    this.xmlFile = xmlFile;
    } /**
     * 创建一个新的XML文件 默认字符集为UTF-8
     * @param root 根元素名
     * @throws Exception
     */
    public void create(String root) throws Exception {
    create(root, charSet);
    } /**
     * 创建一个新的XML文件
     * @param root 根元素名
     * @param charSet 字符集
     * @throws Exception
     */
    public void create(String root, String charSet) throws Exception {
    if (xmlFile.trim().length() == 0) {
    throw new Exception("Can't file name is empty!");
    } // 根元素为空时, 默认为MT
    this.root = new Element(root.trim().length() == 0 ? "MT" : root); // 字符集为空时, 默认为UTF-8
    this.charSet = charSet.trim().length() == 0 ? "UTF-8" : charSet; FileOutputStream xmlWrite = null;
    try {
    document = new Document(this.root);
    XMLOutputter output = new XMLOutputter("", true, charSet);
    xmlWrite = new FileOutputStream(xmlFile);
    output.output(document, xmlWrite);
    } finally {
    xmlWrite.close();
    }
    }
      

  4.   

    /**
     * 打开XML文件,获取根元素
     * @throws Exception
     */
    public void open() throws Exception {
    if (xmlFile.trim().length() == 0) {
    throw new Exception("Can't file name is empty!");
    }
    FileInputStream xmlRead = null;
    try {
    charSet = getEncoding();
    xmlRead = new FileInputStream(xmlFile);
    SAXBuilder readXml = new SAXBuilder();
    document = readXml.build(xmlRead);
    root = document.getRootElement();
    } finally {
    xmlRead.close();
    }
    } /**
     * 根据提供的路径,查找元素
     * @param elePath  路径
     * @param eleName  元素
     * @return Element 元素
     * @throws Exception
     */
    public Element findElement(String elePath, String eleName) throws Exception {
    return findElement(elePath, eleName, 0);
    } /**
     * 根据主键查询记录的索引值
     * @param elePath  元素路径
     * @param eleName 元素名(记录)
     * @param primaryKey [n][2] 主键 0:key 1:value
     * @return int 索引值
     */
    public int getElementIndex(String elePath, String eleName,
    String[][] primaryKey) {
    try {
    int size = findElement(elePath).getChildren(eleName).size();
    boolean isTrue;
    for (int i = 0; i < size; i++) {
    isTrue = true;
    HashMap map = getElementAttribs(elePath, eleName, i);
    for (int j = 0; j < primaryKey.length; j++) {
    if (!primaryKey[j][1].equals(map.get(primaryKey[j][0])
    .toString())) {
    isTrue = false;
    break;
    }
    }
    if (isTrue) {
    return i;
    }
    }
    } catch (Exception e) {
    }
    return -1;
    }
      

  5.   

    /**
     * 根据提供的路径,查找第index个元素
     * @param elePath 路径
     * @param eleName  元素
     * @param index  索引
     * @return Element
     * @throws Exception
     */
    public Element findElement(String elePath, String eleName, int index)
    throws Exception {
    Element element = null;
    try {
    element = (Element) findElement(elePath).getChildren(eleName).get(
    index);
    } catch (Exception e) {
    throw e;
    }
    return element;
    } /**
     * 获取元素的当前子元素个数
     * @param elePath 元素路径
     * @param eleName 元素名
     * @return int 元素总数
     */
    public int getChildSize(String elePath, String eleName) {
    try {
    return findElement(elePath, eleName).getChildren().size();
    } catch (Exception e) {
    }
    return 0;
    } /**
     * 获取元素的当前指定名称的子元素个数
     * @param elePath 元素路径
     * @param eleName 元素名
     * @param childName 子元素名
     * @return int 元素总数
     */
    public int getChildSize(String elePath, String eleName, String childName) {
    try {
    return findElement(elePath, eleName).getChildren(childName).size();
    } catch (Exception e) {
    }
    return 0;
    }
      

  6.   

    /**
     * 获取给定的路径值
     * @param elePath 路径
     * @return Element 路径对应的元素
     * @throws Exception
     */
    public Element findElement(String elePath) throws Exception {
    // 根元素为空,则抛出异常
    if (root == null) {
    throw new Exception("Can't found root.");
    } // 路径为空,返回根元素
    if (elePath == null || elePath.trim().length() == 0) {
    return root;
    } // 遍历路径,找到最后一个元素,路径异常抛出错误。
    Element element = root;
    String[] paths = getPaths(elePath);
    for (int i = 0; i < paths.length; i++) {
    element = element.getChild(paths[i]);
    }
    return element;
    } /**
     * 获取元素文本值
     * @param elePath 元素路径
     * @param eleName 元素名
     * @param index 元素索引
     * @return String 文本值
     */
    public String getElementText(String elePath, String eleName, int index) {
    try {
    return findElement(elePath, eleName, index).getText();
    } catch (Exception e) {
    }
    return "";
    }
      

  7.   

    /**
     * 获取元素属性
     * @param elePath 元素路径
     * @param eleName 元素名
     * @param index 元素索引
     * @return HashMap
     */
    public HashMap getElementAttribs(String elePath, String eleName, int index) {
    HashMap map = new HashMap();
    try {
    List attribs = findElement(elePath, eleName, index).getAttributes();
    for (int i = 0; i < attribs.size(); i++) {
    Attribute attrib = (Attribute) attribs.get(i);
    map.put(attrib.getName(), attrib.getValue());
    }
    } catch (Exception e) {
    }
    return map;
    } /**
     * 元素是否存在
     * @param elePath 元素路径
     * @return True: 存在; False: 不存在;
     */
    public boolean isExistElement(String elePath) {
    try {
    if (findElement(elePath) != null) {
    return true;
    }
    } catch (Exception e) {
    }
    return false;
    }
      

  8.   

    /**
     * 把路径转换为数组
     * @param path 路径
     * @return String[]
     */
    private String[] getPaths(String path) {
    if(path == null) {
    return new String[]{};
    }
    path = path.replace('.', '/').replace('\\', '/');
    return path.split("/");
    } /**
     * 获取XML文件设置的字符集, 未设置则默认为"UTF-8"
     * @return String 字符集
     */
    private String getEncoding() {
    BufferedReader reader = null;
    try {
    reader = new BufferedReader(new FileReader(xmlFile));
    String head = reader.readLine();
    if (head.indexOf("encoding") > 0) {
    head = head.substring(head.indexOf("encoding"));
    int index = head.indexOf("\"") + 1;
    return head.substring(index, head.indexOf("\"", index));
    }
    } catch (Exception e) {
    } finally {
    try {
    reader.close();
    } catch (Exception ex) {
    }
    }
    return "UTF-8";
    }
    }
      

  9.   

    什么东西。只能连发三次。还得搞个号子过来分开一下。郁闷的CSDN呀。