没错啊,就是转成XML后用XPath查询很方便的啦

解决方案 »

  1.   

    用xslt应该可以的。
    <?xml version="1.0"?>
    <xsl:stylesheet version='1.0" match="ALLDATA">
    ...
    ...
      

  2.   

    那就开始学习xml了,解决了在结贴大家应该没意见把
      

  3.   

    如果纯粹是为了操纵String才用了xml的话,
    是不是直接用正则表达式来搞会好点?
    如果理解错了,你们继续讨论,当我透明的.呵呵
      

  4.   

    经常做这种操作,那么选择xml是最合适的,
    偶尔的话就可以用正则!
      

  5.   

    用操作xml的方式遍历查询该层节点 搜出符合条件的对象
    方法很多 传统dom sax 或其它解析工具 用xpath的一个例子如下String fileName = "test.xml";        XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();        InputSource is = new InputSource(new FileInputStream(fileName));        XPathExpression xpathExpression = xpath
                    .compile("//title");
            String title = xpathExpression.evaluate(is);
            System.out.println("Title is:" + title);        InputSource inputSource = new InputSource(new FileInputStream(fileName));
            String price = xpath.evaluate("//title[1]/text()", inputSource);
            System.out.println("Price:" + price);        inputSource = new InputSource(new FileInputStream(fileName));
            String expression = "//book/title[last()]";
            NodeList nodeList = (NodeList) xpath.evaluate(expression, inputSource,
                    XPathConstants.NODESET);
            System.out.println("title number:" + nodeList.getLength());
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = (Node) nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    System.out.println("type: " + element.getAttribute("type"));
                }
            }
    例子没有作针对修改 仅作参考 楼主多去查查相关内容
      

  6.   

    您的意思是一段String是xml内容,想转化成xml对象是吧,我用的是dom4j
    String content = "<root><a>this is test</a></root>";
    Reader reader = new BufferedReader(new StringReader(content));
    SAXReader saxReader = new SAXReader(false);
    Document doc = saxReader.read(reader);
    但要注意,这样做一定要有根目录。
      

  7.   

    public static  void main(String[] args)throws Exception{
      
            StringBuffer stuff=new StringBuffer();
            stuff.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            stuff.append("<bookList><book><name>Java编程入门</name><author>张三</author><publishDate>2002-6-6</publishDate><price>35.0</price></book><book><name>XML在Java中的应用</name><author>李四</author><publishDate>2002-9-16</publishDate><price>92.0</price></book></bookList>");
            SAXBuilder  sb=new SAXBuilder();// 新建立构造器 
            Reader in= new StringReader(stuff.toString());
            Document doc=sb.build(in);// 读入文件 
            Element root = doc.getRootElement(); // 获得根元素element 
            String strPath="//book/author='张三'";
            List find = XPath.selectNodes(root, strPath);
               System.out.println("size="+find.size());
            for(int i=0;i<find.size();i++){
            
            Element ft = (Element) find.get(i); 
            System.out.println(ft.getAttributeValue("publishDate"));
            } 
     }我想查询author=张三的publishDate,为什么会有问题