Parsing xml file contents without knowing xml file structure

前端 未结 1 465
感情败类
感情败类 2021-01-02 05:21

I\'ve been working on learning some new tech using java to parse files and for the msot part it\'s going well. However, I\'m at a lost as to how I could parse an xml file to

相关标签:
1条回答
  • 2021-01-02 05:39

    Well the parsing part is easy; like helderdarocha stated in the comments, the parser only requires valid XML, it does not care about the structure. You can use Java's standard DocumentBuilder to obtain a Document:

    InputStream in = new FileInputStream(...);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
    

    (If you're parsing multiple documents, you can keep reusing the same DocumentBuilder.)

    Then you can start with the root document element and use familiar DOM methods from there on out:

    Element root = doc.getDocumentElement(); // perform DOM operations starting here.
    

    As for processing it, well it really depends on what you want to do with it, but you can use the methods of Node like getFirstChild() and getNextSibling() to iterate through children and process as you see fit based on structure, tags, and attributes.

    Consider the following example:

    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import javax.xml.parsers.DocumentBuilderFactory;   
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    
    
    public class XML {
    
        public static void main (String[] args) throws Exception {
    
            String xml = "<objects><circle color='red'/><circle color='green'/><rectangle>hello</rectangle><glumble/></objects>";
    
            // parse
            InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
    
            // process
            Node objects = doc.getDocumentElement();
            for (Node object = objects.getFirstChild(); object != null; object = object.getNextSibling()) {
                if (object instanceof Element) {
                    Element e = (Element)object;
                    if (e.getTagName().equalsIgnoreCase("circle")) {
                        String color = e.getAttribute("color");
                        System.out.println("It's a " + color + " circle!");
                    } else if (e.getTagName().equalsIgnoreCase("rectangle")) {
                        String text = e.getTextContent();
                        System.out.println("It's a rectangle that says \"" + text + "\".");
                    } else {
                        System.out.println("I don't know what a " + e.getTagName() + " is for.");
                    }
                }
            }
    
        }
    
    }
    

    The input XML document (hard-coded for example) is:

    <objects>
        <circle color='red'/>
        <circle color='green'/>
        <rectangle>hello</rectangle>
        <glumble/>
    </objects>
    

    The output is:

    It's a red circle!
    It's a green circle!
    It's a rectangle that says "hello".
    I don't know what a glumble is for.
    
    0 讨论(0)
提交回复
热议问题