简体   繁体   中英

Reading Multiple XML FILES with attributes java

I have to read 100 XML, I need to read in java. This code only reads one XML but how do I read a whole folder of XML?. I used XPath because all the XML has attributes. All the XML in the folder are bills and I need to obtain specific information about each one:

public class Interpretarxml {

    public static void main(String[]args){
        try{
            DocumentBuilderFactory db= DocumentBuilderFactory.newInstance();
            //Agilizar la lectura de arhivos grandes
            db.setNamespaceAware(false);
            db.setValidating(false);
            db.setFeature("http://xml.org/sax/features/namespaces", false);
            db.setFeature("http://xml.org/sax/features/validation", false);
            db.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            db.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            //Constructor de Objetos
            DocumentBuilder a= db.newDocumentBuilder();
            //Ruta del Archivo
            String archivo="C:\\Users\\carme\\OneDrive\\Escritorio\\Documentos\\NetBeansProjects\\Practicarxml\\src\\main\\java\\LeerXml\\1b254147-26f4-4ca9-8f85-3d5da753dc99.xml";
            File f=new File(archivo);    
            //Objeto Documento XML
            Document doc= a.parse(f);
            doc.getDocumentElement().normalize();
    
            XPath x = XPathFactory.newInstance().newXPath();
            String Tras="/Comprobante";
    
            NodeList n = (NodeList)x.compile(Tras).evaluate(doc,XPathConstants.NODESET);
            System.out.println("Cantidad de elementos que empatan con la ruta " + n.getLength());
            Element e = (Element)n.item(0);
            System.out.println("Tipo\t\t: " + e.getAttribute("TipoDeComprobante"));
            System.out.println("Total\t\t: " + e.getAttribute("Total"));
            System.out.println("Subtotal\t\t: " + e.getAttribute("SubTotal"));
            System.out.println("Metodo de Pago: " + e.getAttribute("MetodoPago"));
           
            String Em = "/Comprobante/Emisor";
            NodeList nodeh=(NodeList)x.compile(Em).evaluate(doc,XPathConstants.NODESET);
            for (int i = 0; i < nodeh.getLength(); i++) {
                Node u=nodeh.item(i);
                if(u.getNodeType()==Node.ELEMENT_NODE){
                    Element d=(Element)u;
                    System.out.println("Nombre\t\t:" + d.getAttribute("Nombre"));
                    System.out.println("RFC\t\t:" + d.getAttribute("Rfc"));
                }
            }
            String Con="/Comprobante/Conceptos/Concepto";
            NodeList nodeC = (NodeList)x.compile(Con).evaluate(doc,XPathConstants.NODESET);
            System.out.println("Cantidad de Conceptos de la Factura");
            System.out.println(nodeC.getLength());
            System.out.println( "" );
           
            for (int i = 0; i < nodeC.getLength(); i++) {
                Node nodo= nodeC.item(i);
                if(nodo.getNodeType()==Node.ELEMENT_NODE){
                    Element s= (Element)nodo;
                    System.out.println("ClaveProdServ\t\t: " + s.getAttribute("ClaveProdServ"));
                    System.out.println("ClaveUnidad\t\t: " + s.getAttribute("ClaveUnidad"));
                    System.out.println("Descripciont\t\t: " + s.getAttribute("Descripcion"));
                    System.out.println("ValorUnitario\t\t: " + s.getAttribute("ValorUnitario"));
                    System.out.println("NoIdentificacion\t: " + s.getAttribute("NoIdentificacion"));
                    System.out.println("\n");
                }
            }  
        }
        catch(IOException | ParserConfigurationException | 
                            XPathExpressionException | SAXException e) {
            Logger.getLogger(Interpretarxml.class.getName()).log(Level.SEVERE, null, e);  
        }
    
    }
}
Path directory = Paths.get("/path/to/xml/folder/");
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**.{xml,XML}");
Files.find(directory, 1, (p, u) -> pathMatcher.matches(p))
        .map(Path::toFile)
        .forEach(System.out::println);

Replace the call to System.out.println() with a call to your existing XML parsing code. If you don't need to support the uppercase XML file extension, just use "glob:**.xml" as argument to getPathMatcher() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM