【问题标题】:parsing XML file in java using SAX parser使用 SAX 解析器在 java 中解析 XML 文件
【发布时间】:2014-01-12 20:29:58
【问题描述】:

我想用java编写一个程序并使用saxparser来解析一个像link这样的XML文件 执行以下操作; 从输入接收 ID 并在 XML 文件中解析和搜索并写入 texttitleusername (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 文件输入示例。

标签: java saxparser


【解决方案1】:

代码看起来

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

比相对文件更好。

对于长文本characters 可能会被多次调用。

解析后丢失(我认为):

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

备注:setId/getId 大写为约定。

【讨论】:

    【解决方案2】:

    谢谢大家(特别是 Joop Eggen)

    我成功更改了我的代码

    看...

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

    这只是另一个问题,将输出写入文件而不是在控制台中打印的最佳方法是什么?

    【讨论】:

    • PrintWriter out = new PrintWriter(file, "UTF-8");
    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2011-05-31
    • 2011-04-30
    • 2017-08-19
    • 2011-06-17
    相关资源
    最近更新 更多