关于文件的解析??

zyylily 2003-08-22 12:12:56
一个文件结构形如XML,如何找到与指定标签下的内容值相匹配的节点,并把该节点下的其他内容打印出来??
例如,文件包含如下内容
...........
<rdf:Description rdf:about="MusicianLightning_Seeds">
<rdf:type>Musician</rdf:type>
<rdf:type>MusicGroup</rdf:type>
<rdfs:label xml:lang="en">Lightning Seeds</rdfs:label>
<sortPriority>10</sortPriority>
<hasMember>MusicianSharrock,_Chris</hasMember>
<hasMember>MusicianRogers,_Simon</hasMember>
<hasMember>MusicianBroudie,_Ian</hasMember>
</rdf:Description>
<rdf:Description rdf:about="MusicianLucky_Bishops">
<rdf:type>Musician</rdf:type>
<rdfs:label xml:lang="en">Lucky Bishops</rdfs:label>
</rdf:Description>
............
现有一查询字符串str="Lightning Seeds",根据该字符串查找指定标签<rdfs:label>内的内容,如果他们相匹配(即查找到相关的内容)则需要将<rdf:Description >标签内的内容全部输出,即根据Lightning Seeds,上述应该得到的结果是:
Musician
MusicGroup
Lightning Seeds
10
MusicianSharrock,_Chris
MusicianRogers,_Simon
MusicianBroudie,_Ian
请问这样的要求在java下应该怎样做才能实现??给个思路也行哪!!
...全文
70 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyylily 2003-08-23
  • 打赏
  • 举报
回复
呵呵,终于解决了!!
我犯了个逻辑错误^-^.写在endElement里就可以了
anyway,谢谢!!
zyylily 2003-08-23
  • 打赏
  • 举报
回复
谢谢rex0y(的开发绝望) 的建议!!
我写了下面的一段代码,想要打印出其中符合查询字符串的类型(即<rdf;type></type>下的内容),不过不知道为什么,content中的内容老是不能赋值给content1,所以每次都找不到匹配的字符串,strtype的值也总是为空???
大家帮忙看看吧,java里头string和StringBuffer难道不能够直接用=号赋值吗?
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

class LocalRDF extends DefaultHandler
{
private boolean IsInDescrption=false;
String strtype="";
private StringBuffer content1 =new StringBuffer();
public LocalRDF(){super();}

public void startDocument(){System.out.println("*****************");}

public void startElement(String uri, String localName, String qName, Attributes attrs){
if(qName.equals("rdf:Description" )){
IsInDescrption=true;
}
else if(qName.equals("rdf:type" )&&IsInDescrption){
strtype=content1.toString();
}
else if(qName.equals("rdfs:label" )){
if(content1.toString().indexOf("upper eye lid")!=-1
System.out.println("I found it!! The type is:"+strtype);}
}

}

public void characters(char[] ch,int start,int length) throws SAXException
{
StringBuffer content= new StringBuffer();
content.append(ch,start,length);
content1=content;
if(content.toString().indexOf("upper eye lid")!=-1){
System.out.println("I found it!!"+content.toString());
}
}


public void endElement(String uri, String localName, String qName)
{
if(qName.equals("rdf:Description" )){
boolean IsInDescrption=false;
}
}
public void endDocument(){System.out.println("*******the end**********");}

public static void main(String[] args)
{

try{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
LocalRDF reader = new LocalRDF();
sp.parse(new InputSource("tap.rdf"),reader);
}
catch(Exception e){
e.printStackTrace();
}
}
}
rex0y 2003-08-22
  • 打赏
  • 举报
回复
用sax 吧
public void startElement(String namespaceURI, String localName,String qName, Attributes attributes)
//记下当前的元素名字 等等
this.qName = qName;
this.namespaceURI = namespaceURI;
if(namespaceURI.equals("rdf") ){
if (qName.equals("Description ")){
attributes.getValue("about") ;
if(about.indexOf("Lightning Seeds")!= -1){
//找到
}
}
}

public void characters(char[] ch, int start, int length)
throws SAXException {

StringBuffer currentValue = new StringBuffer();
currentValue.append(ch, start, length);
if(currentValue.toString().indexOf("...") != -1){
//找到
}


}

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧