Domanda

I Want to write a program in java and use saxparser to pars an XML file like this link which do the following; receive an ID from input and parse and search in XML file and writes text and title and username (XML tags) specified for received ID (just *ID*s after ns tag is my meaning)in a file. and program suppose to do the same for four other IDs

and need your help...

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());
        }
    }   



}

public class 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>";
}
}

and I expect a result like this for each ID:

<Page>
     <title>AccessibleComputing</title>  
     <id>654982</id>
     <text>#REDIRECT [[Computer accessibility]] {{R from CamelCase}}</text>
     <username>Xqbot</username>
</Page>
È stato utile?

Soluzione 2

Thank all of you (specially Joop Eggen)

I successfully change my codes

see...

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 your next ID:\n");

                try {
                    Scanner sc = new Scanner(System.in);

                    ID_number_1 = sc.nextInt();
                    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");
                }

                /*
                 * 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);

                    System.out.println(handler.getFwkList());

                } catch (ParserConfigurationException | SAXException | IOException e) {
                    e.printStackTrace();
            }
            /*
             * 
            */
            System.out.println("writing in file "+ID_number_1);
        }
    }
}

public class MyProjectHandler extends DefaultHandler {


    FAWiki wiki = new FAWiki() ;
    String titlestr = null;
    String textstr = null;
    String usernamestr = null;

    boolean flag1 = false;
    boolean flag2 = false;
    boolean bid = false;
    boolean btitle = false;
    boolean btext = false;
    boolean busername = false;
    boolean match = false;
    boolean bns = false;
    boolean flag3 = false;

    public String getFwkList() {
        return wiki.toString(wiki , match);
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase("title")) {btitle = true;}

        if(qName.equalsIgnoreCase("ns")) {bns = true;}

        if(qName.equalsIgnoreCase("id")) {bid = true;}

        if(qName.equalsIgnoreCase("text")) {btext = true;}

        if(qName.equalsIgnoreCase("username")) {busername = true;}
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {}

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

        if(btitle) {
            titlestr = new String(ch, start, length);
            btitle= false;
        }

        if(bid && bns) {

            int temp = Integer.parseInt(new String(ch, start, length));
            if(ReadXMLFile.ID_number_1 == temp) {

                wiki.setid(temp);
                wiki.settitle(titlestr);
                match = true;
                btext = false;
                flag1 = true;
                busername = false;
                flag2 = true;
            }    

            bns = false;
        }

        bid = false;

        if(btext && flag1) {
            textstr = new String(ch, start, length);
            wiki.settext(textstr);
            btext = false;
            flag1 = false;
        }

        if(busername && flag2) {
            usernamestr = new String(ch, start, length);
            wiki.setUsername(usernamestr);
            busername = false;
            flag2 = false;
        }
    }   
}

public class FAWiki {

    private String title;
    private int id;
    private String text;
    private String username;

    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 getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String toString(FAWiki fwik , Boolean match) {
        if(match) {
            return "<page>\n"+"\t<title>"+fwik.gettitle()+"</title>\n"+"\t<id>"+fwik.getid()+"</id>\n"+"\t<text>"+fwik.gettext()+"</text>\n"+"\t<username>"+fwik.getUsername()+"</username>\n"+"</page>";
        } else {
            return  "Your entered id doesn't match";
        }
    }
}

Here is just another problem what is the best way to write the output in a file instead of printing in console?

Altri suggerimenti

The code looks

InputStream in = getClass().getResourceAsStream("/SAX-XML-FAWiki.xml"); saxParser.parse(in, handler);

is better instead of a relative file.

For long text characters might be called more than once.

Missing after parse (I think):

String wiki = handler.getFwklist();
Path path = Paths.get(ID_number_1);
Files.write(path, wiki.getBytes(StandardCharsets.UTF_8,
    StandardOpenOptions.CREATE, StandardOpenOptions.WRITE);

Remark: setId/getId with capital is convention.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top