【发布时间】:2014-01-12 20:29:58
【问题描述】:
我想用java编写一个程序并使用saxparser来解析一个像link这样的XML文件 执行以下操作; 从输入接收 ID 并在 XML 文件中解析和搜索并写入 text 和 title 和 username (XML 标记)为文件中接收到的 ID 指定(我的意思是 ns 标记之后的 *ID*s)。 并且程序假设对其他四个 ID 执行相同操作
需要你的帮助...
public class ReadXMLFile {
public static int ID_number_1 ;
public static void main(String[] args) {
for(int x=1; x<=5; x++){
if(x==1)System.out.println("enter an integer as ID:\n");
else System.out.println("enter another ID:\n");
try{
Scanner sc = new Scanner(System.in);
ID_number_1 = sc.nextInt();
/*
* some process happening here;
*/
SAXParserFactory factory = SAXParserFactory.newInstance();
try{
SAXParser saxParser = factory.newSAXParser();
MyProjectHandler handler = new MyProjectHandler();
saxParser.parse("src\\SAX-XML-FAWiki.xml", handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
/*
*
*/
System.out.println("writing in file "+ID_number_1);
switch (x) {
case 1:
System.out.println("we got your first id :"+ID_number_1);
break;
case 2:
System.out.println("we got your second id :"+ID_number_1);
break;
case 3:
System.out.println("we got your third id :"+ID_number_1);
break;
case 4:
System.out.println("we got your fourth id :"+ID_number_1);
break;
case 5:
System.out.println("we got your fifth id :"+ID_number_1);
break;
}
}catch (Exception e) {
System.out.println("You should enter a valid integer");
}
}
}
}
public class MyProjectHandler extends DefaultHandler {
private FAWiki wiki = null;
public String getFwkList() {
return wiki.toString();
}
boolean bid = false;
boolean btitle = false;
boolean btext = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException{
if(qName.equalsIgnoreCase("id")){
bid = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("id")) {
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException{
if(bid){
int temp = Integer.parseInt(new String(ch, start, length));
if(ReadXMLFile.ID_number_1 == temp){
wiki = new FAWiki();
wiki.setid(temp);
btitle = true;
btext = true;
}
}
if(btitle){
wiki.settitle(new String(ch, start, length));
btitle = false;
}
if(btext){
wiki.settext(new String(ch, start, length));
btext = false;
System.out.println(getFwkList());
}
}
}
公开课 FAWiki {
private String title;
private int id;
private String text;
public String gettitle(){
return title;
}
public void settitle(String title){
this.title = title;
}
public int getid(){
return id;
}
public void setid(int id){
this.id = id;
}
public String gettext(){
return text;
}
public void settext(String text){
this.text = text;
}
public String toString(){
return "<page>\n"+"\t<title>"+this.title+"</title>\n"+"\t<id>"+this.id+" </id>\n"+"\t<text>"+this.text+"</text>\n"+"</page>";
}
}
我希望每个 ID 都有这样的结果:
<Page>
<title>AccessibleComputing</title>
<id>654982</id>
<text>#REDIRECT [[Computer accessibility]] {{R from CamelCase}}</text>
<username>Xqbot</username>
</Page>
【问题讨论】:
-
你试过什么?你读过Oracle Tutorial SAX吗?
-
您应该将上面的代码粘贴到代码“标签”中!
-
您提供的链接既没有给出您作为评论提供的代码的结果,也没有给出您在问题中提到的任何示例。
-
在您的问题中提供完整的详细信息,您尝试过的代码是什么以及您面临的问题是什么?
-
如果链接有问题,假设一个维基百科转储文件作为 XML 文件输入示例。