没错啊,就是转成XML后用XPath查询很方便的啦
解决方案 »
- 用java写了一个记事本的字体对话框,可是当改变字体类型(style)时总不改变,不知道是哪里的问题,大家帮忙看看
- hibernate3 读取数据问题 (怎么读取单字段)
- 急啊!Spring的问题!大家看着给解决一下吧
- spring mvc dao层多表查询
- 小弟弟我是初学,关于方法的添加
- 多线程大牛请进!
- 如何利用javamail api修改一封邮件的Header字段
- cmp不能插入数据!!!!
- 送30分,只要您的weblogic7.0是运行正常的!....内!
- 帮帮忙了
- HQL语句,如何同时删除多行记录?
- Hibernate中插入数据时出现的问题,调一天都没出来,请大家帮帮忙解决一下!!!!
<?xml version="1.0"?>
<xsl:stylesheet version='1.0" match="ALLDATA">
...
...
是不是直接用正则表达式来搞会好点?
如果理解错了,你们继续讨论,当我透明的.呵呵
偶尔的话就可以用正则!
方法很多 传统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"));
}
}
例子没有作针对修改 仅作参考 楼主多去查查相关内容
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);
但要注意,这样做一定要有根目录。
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,为什么会有问题