请教如何query XML 文档? 这个恐怕不是xml的强项,如果一定要实现要比通过数据库要麻烦,你可以动态生成xsl,通过xsl的条件判断选择需要输出的节点值。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static Document parseXML(InputStream input) throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = null; synchronized(com.justnorth.util.Tool.class) { if(docFactory == null) docFactory = DocumentBuilderFactory.newInstance(); } builder = docFactory.newDocumentBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF8")); InputSource is = new InputSource(br); return builder.parse(is); } public static Document parseXML(String input) throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = null; synchronized(com.justnorth.util.Tool.class) { if(docFactory == null) docFactory = DocumentBuilderFactory.newInstance(); } builder = docFactory.newDocumentBuilder(); //BufferedReader br = new BufferedReader(new InputStreamReader(input, "gb2312")); InputSource is = new InputSource(new StringReader(input)); return builder.parse(is); } public static String getAttribute(Element elem, String name) { Attr attr = elem.getAttributeNode(name); return attr != null ? attr.getValue() : null; } 谢谢楼上的两位 To;signboy(横) 谢谢你的代码,不过你好象误解我意思了,可能是我没说明白。。 我的意思是通过已知的多个条件,查找出XML 中符合所有条件的结点。。 如果单纯用XQUERY , XQL 应该能做到,不过,不太熟悉XQUERY 和XQL 是否有java的接口?? 要在其中加上验证和返值的过程。。已经有开发好的SCHEMA 是我的毕业设计中的一部分,不知道能否实现?? select.xml:<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="select.xsl"?><select> <service> <type>sendmail</type> <mailto>[email protected]</mailto> <from>[email protected]</from> </service> <service> <type>upload</type> <filename>xml.pdf</filename> <size>1024K</size> </service></select> select.xsl:<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"><html> <head> <title>select result</title> </head> <body> <xsl:for-each select="select/service"> <xsl:choose select="type"> <xsl:when test="type='sendmail'"> <b><xsl:value-of select="mailto"/></b><br/> <b><xsl:value-of select="from"/></b><br/> </xsl:when> <xsl:otherwise> <i><xsl:value-of select="filename"/></i><br/> <i><xsl:value-of select="size"/></i><br/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </body></html></xsl:template></xsl:stylesheet>xsl文件你可以通过程序动态的生成,来满足你的查询条件 还可以用<xsl:if test="statement"></xsl:if>来进行判断输出 struts2配置文件报错了!求助! list和map问题,请教 ejb+spring+struts整合-----在线急等 一个关于jms的问题 项目描述 java 替换word linux 请教一个JSF中在dataTable中<h:commandLink/>失效的问题 请高手帮我看一下 如何取得spring的配置文件applicationContext.xml 请大家帮下忙!hibernate中(oracle)blob字段怎么读写啊? Jbuilder8编辑器的中文显示乱码啊? 对多数据库查询的问题。在线等待,立即给分
throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException
{
DocumentBuilder builder = null;
synchronized(com.justnorth.util.Tool.class)
{
if(docFactory == null)
docFactory = DocumentBuilderFactory.newInstance();
}
builder = docFactory.newDocumentBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF8"));
InputSource is = new InputSource(br);
return builder.parse(is);
}
public static Document parseXML(String input)
throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException
{
DocumentBuilder builder = null;
synchronized(com.justnorth.util.Tool.class)
{
if(docFactory == null)
docFactory = DocumentBuilderFactory.newInstance();
}
builder = docFactory.newDocumentBuilder();
//BufferedReader br = new BufferedReader(new InputStreamReader(input, "gb2312"));
InputSource is = new InputSource(new StringReader(input));
return builder.parse(is);
}
public static String getAttribute(Element elem, String name)
{
Attr attr = elem.getAttributeNode(name);
return attr != null ? attr.getValue() : null;
}
谢谢你的代码,不过你好象误解我意思了,可能是我没说明白。。
我的意思是通过已知的多个条件,查找出XML 中符合所有条件的结点。。 如果单纯用XQUERY , XQL 应该能做到,不过,不太熟悉XQUERY 和XQL 是否有java的接口?? 要在其中加上验证和返值的过程。。已经有开发好的SCHEMA 是我的毕业设计中的一部分,不知道能否实现??
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="select.xsl"?>
<select>
<service>
<type>sendmail</type>
<mailto>[email protected]</mailto>
<from>[email protected]</from>
</service>
<service>
<type>upload</type>
<filename>xml.pdf</filename>
<size>1024K</size>
</service>
</select>
select.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
>
<xsl:template match="/">
<html>
<head>
<title>select result</title>
</head>
<body>
<xsl:for-each select="select/service">
<xsl:choose select="type">
<xsl:when test="type='sendmail'">
<b><xsl:value-of select="mailto"/></b><br/>
<b><xsl:value-of select="from"/></b><br/>
</xsl:when>
<xsl:otherwise>
<i><xsl:value-of select="filename"/></i><br/>
<i><xsl:value-of select="size"/></i><br/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>xsl文件你可以通过程序动态的生成,来满足你的查询条件