Search in sources :

Example 36 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project opentheso by miledrousset.

the class testimportxml method ouvreFichier.

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
// 
@Test
public void ouvreFichier() {
    HikariDataSource conn = openConnexionPool();
    SAXBuilder builder = new SAXBuilder();
    ArrayList<Table> toutTables = new ArrayList<>();
    ArrayList<LineOfData> lineOfDatas = new ArrayList<>();
    File xmlFile = new File("C:/Users/antonio.perez/Desktop/testbon.xml");
    try {
        // Se crea el documento a traves del archivo
        Document document = (Document) builder.build(xmlFile);
        // Se obtiene la raiz 'tables'
        Element rootNode = document.getRootElement();
        // Se obtiene la lista de hijos de la raiz 'tables'
        // ici on a toutes les tables (les enfants de la racine)
        List list = rootNode.getChildren("table");
        // Se recorre la lista de hijos de 'tables'
        for (int i = 0; i < list.size(); i++) {
            // Se obtiene el elemento 'tabla'
            // ici on a la première table
            Element tabla = (Element) list.get(i);
            // Se obtiene el atributo 'nombre' que esta en el tag 'tabla'
            // ici on a le nom de la table
            String nombreTabla = tabla.getAttributeValue("nom");
            System.out.println("Nom de la table : " + nombreTabla);
            // Se obtiene la lista de hijos del tag 'tabla'
            // ici c'est la liste des lignes de la table
            List lista_campos = tabla.getChildren();
            // ici on découpe la liste des lignes
            for (int j = 0; j < lista_campos.size(); j++) {
                // Se obtiene el elemento 'campo'
                // ici on a une ligne de la table
                Element campo = (Element) lista_campos.get(j);
                // System.out.println("nouvelle ligne table "+ nombreTabla);
                for (Element colonne : campo.getChildren()) {
                    LineOfData lineOfData = new LineOfData();
                    // System.out.println("Nom de la colonne = " + colonne.getName());
                    // System.out.println("valeur de la colonne = " + colonne.getText());
                    lineOfData.setColomne(colonne.getName());
                    lineOfData.setValue(colonne.getText());
                    lineOfDatas.add(lineOfData);
                }
                insertLine(conn, lineOfDatas, nombreTabla);
                lineOfDatas.clear();
            }
        // / mettre à jour la table dans la BDD
        }
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Table(mom.trd.opentheso.core.exports.privatesdatas.tables.Table) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) LineOfData(mom.trd.opentheso.core.exports.privatesdatas.LineOfData) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 37 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project k-9 by k9mail.

the class SettingsExporterTest method parseXML.

private Document parseXML(byte[] xml) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    InputStream stream = new ByteArrayInputStream(xml);
    return builder.build(stream);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 38 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project pcgen by PCGen.

the class Initiative method loadINIT.

/**
	 * Perform initial loading
	 * @param initFile
	 * @param comp
	 */
public void loadINIT(File initFile, PCGenMessageHandler comp) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document character = builder.build(initFile);
        loadFromDocument(character, comp);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), "File load error: " + initFile.getName());
        Logging.errorPrint("File Load Error" + initFile.getName());
        Logging.errorPrint(e.getMessage(), e);
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Document(org.jdom2.Document) IOException(java.io.IOException)

Example 39 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.

the class XmlFile method getBuilder.

public static SAXBuilder getBuilder(Validate validate) {
    // should really be a Verify enum
    SAXBuilder builder;
    boolean verifyDTD = (validate == Validate.CheckDtd) || (validate == Validate.CheckDtdThenSchema);
    boolean verifySchema = (validate == Validate.RequireSchema) || (validate == Validate.CheckDtdThenSchema);
    // old style 
    // argument controls DTD validation
    builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", verifyDTD);
    // insert local resolver for includes, schema, DTDs
    builder.setEntityResolver(new JmriLocalEntityResolver());
    // configure XInclude handling
    builder.setFeature("http://apache.org/xml/features/xinclude", true);
    builder.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
    // only validate if grammar is available, making ABSENT OK
    builder.setFeature("http://apache.org/xml/features/validation/dynamic", verifyDTD && !verifySchema);
    // control Schema validation
    builder.setFeature("http://apache.org/xml/features/validation/schema", verifySchema);
    builder.setFeature("http://apache.org/xml/features/validation/schema-full-checking", verifySchema);
    // if not validating DTD, just validate Schema
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", verifyDTD);
    if (!verifyDTD) {
        builder.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
    }
    // allow Java character encodings
    builder.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
    return builder;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) JmriLocalEntityResolver(jmri.util.JmriLocalEntityResolver)

Example 40 with SAXBuilder

use of org.jdom2.input.SAXBuilder in project JMRI by JMRI.

the class XmlFileTest method testProcessPI.

public void testProcessPI() throws org.jdom2.JDOMException, java.io.IOException {
    // Document from test file
    Document doc;
    Element e;
    FileInputStream fs = new FileInputStream(new File("java/test/jmri/jmrit/XmlFileTest_PI.xml"));
    try {
        // argument controls validation
        SAXBuilder builder = XmlFile.getBuilder(XmlFile.Validate.None);
        doc = builder.build(new BufferedInputStream(fs));
        Assert.assertNotNull("Original Document found", doc);
        e = doc.getRootElement();
        Assert.assertNotNull("Original root element found", e);
        XmlFile x = new XmlFile() {
        };
        Document d = x.processInstructions(doc);
        Assert.assertNotNull(d);
        // test transform changes <contains> element to <content>
        e = d.getRootElement();
        Assert.assertNotNull("Transformed root element found", e);
        Assert.assertTrue("Transformed root element is right type", e.getName().equals("top"));
        Assert.assertTrue("Old element gone", e.getChild("contains") == null);
        Assert.assertTrue("New element there", e.getChild("content") != null);
        Assert.assertTrue("New element has content", e.getChild("content").getChildren().size() == 2);
    } catch (java.io.IOException ex) {
        throw ex;
    } catch (org.jdom2.JDOMException ex) {
        throw ex;
    } finally {
        fs.close();
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) BufferedInputStream(java.io.BufferedInputStream) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

SAXBuilder (org.jdom2.input.SAXBuilder)60 Document (org.jdom2.Document)35 Element (org.jdom2.Element)21 Test (org.junit.Test)20 IOException (java.io.IOException)14 File (java.io.File)12 JDOMException (org.jdom2.JDOMException)10 Modification (com.thoughtworks.go.domain.materials.Modification)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 BufferedInputStream (java.io.BufferedInputStream)5 ParseException (java.text.ParseException)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ModifiedFile (com.thoughtworks.go.domain.materials.ModifiedFile)3 NoProcessSpecifiedException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NoProcessSpecifiedException)3 NotAuthorizedToOverrideException (de.hpi.bpt.scylla.creation.SimulationConfiguration.SimulationConfigurationCreator.NotAuthorizedToOverrideException)3 FileNotFoundException (java.io.FileNotFoundException)3