日期:2014-05-20  浏览次数:20894 次

java中解析xml字符串,顶即有分
解析xml类型的字符串对我来说是很重要的一部分
String str = "<service><sid>1</sid><name>新闻发布服务</name><port>1599</port></service>";
因为项目中要多次用到解析字符串的方法,麻烦各位给支个招,简单方便快捷的方法是最好的啦

------解决方案--------------------
先弄清xml的数据结构,就很简单了。
Java code


/**
     * 建立数据文件的读取连接
     * 
     * @return 读取成功时返回Document对象,失败则返回空
     */
    public static Document getDocument() {

        Document doc1 = null;
        
        File file = new File("xml_path");
        SAXReader xml_read = new SAXReader();
        try {
            doc1 = xml_read.read(file);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return doc1;

    }


private Element search_service(Document doc, String id) {

        Element root = doc.getRootElement();
        String xml_path = "/service/sid";
        Node node = root.selectSingleNode(xml_path);
        if (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }

        return null;
    }
获取xml的数据:
public void getData(){
doc = getDocument();
        Element e = search_service(doc, id);
        String name=e.elementText("name");
///还要获得其他结点信息
        

}

------解决方案--------------------
在项目下建立一个xml文件 试着解析一下
Java code
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Xml {
    public static void main(String[] args) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File("./新建 文本文档.xml"));
            NodeList list = document.getElementsByTagName("Header");
            for (int i = 0; i < list.getLength(); i++) {
                System.out.println(list.item(i).getFirstChild().getNodeValue());
            }
        } catch (Exception e) {
        }
    }
}

------解决方案--------------------
一般如果是固定标签的话我用digester,用起来也方便,写好rule文件后,不用操作每个结点了
提供一段代码
rules.xml
XML code
  <?xml version="1.0" encoding="UTF-8" ?> 
 <digester-rules>
 <pattern value="foo">
  <object-create-rule classname="example.Foo" /> 
  <set-properties-rule /> 
  <bean-property-setter-rule pattern="name" /> 
  <bean-property-setter-rule pattern="age" /> 
  <pattern value="bar">
  <object-create-rule classname="example.Bar" /> 
  <set-properties-rule /> 
  <bean-property-setter-rule pattern="id" /> 
  <bean-property-setter-rule pattern="title" /> 
  <set-next-rule methodname="setBars" /> 
<pattern value="childList">
<pattern value="child">
  <object-create-rule classname="example.Child" /> 
  <set-properties-rule /> 
  <bean-property-setter-rule pattern="sex" /> 
  <bean-property-setter-rule pattern="school" /> 
  <set-next-rule methodname="addChild" /> 
  </pattern>
  </pattern>
  </pattern>
  </pattern>
  </digester-rules>

------解决方案--------------------
顶!!
------解决方案--------------------
dom4j解析xml文件,很简单!网上很多例子!
------解决方案--------------------