Search in sources :

Example 71 with SAXReader

use of org.dom4j.io.SAXReader in project fmv by f-agu.

the class Project method load.

/**
 * @throws LoadException
 */
public void load() throws LoadException {
    SAXReader reader = new SAXReader();
    reader.setEncoding("UTF-8");
    try {
        Document document = reader.read(getSaveFile());
        Element root = document.getRootElement();
        loadMain(root);
        loadSources(root);
        loadExtensions(root);
        loadProperties(root);
        loadUndeleteFile(root);
        loadOutputInfos(root);
        loadExecutables(root);
    } catch (DocumentException e) {
        throw new LoadException(e);
    }
    modified = false;
    fileCache.clean();
    fileCache.start();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 72 with SAXReader

use of org.dom4j.io.SAXReader in project sagacity-sqltoy by chenrenfei.

the class XMLUtil method getXPathElement.

/**
 * @todo 根据qName 获取节点对象
 * @param xmlFile
 * @param qName
 * @return
 * @throws Exception
 */
public static Object getXPathElement(File xmlFile, String qName) throws Exception {
    SAXReader saxReader = new SAXReader();
    InputStream is = new FileInputStream(xmlFile);
    org.dom4j.Document doc = saxReader.read(is);
    return doc.getRootElement().selectObject(qName);
}
Also used : SAXReader(org.dom4j.io.SAXReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream)

Example 73 with SAXReader

use of org.dom4j.io.SAXReader in project notes by KevinBlandy.

the class Dom4jDemo method getLast.

/**
 * ��ȡָ��Ԫ�ص����һ��ֵ(name)
 */
public static void getLast() throws Exception {
    // 1,��������������
    SAXReader reader = new SAXReader();
    // 2,����xml�ļ�
    Document document = reader.read(file);
    // 3,��ȡ���ڵ�
    Element root = document.getRootElement();
    // 3,�Ķ����е�p�ڵ�
    List<Element> list = root.elements("p");
    // 3,��ȡ���һ��p�ڵ�
    Element ele = list.get(list.size() - 1);
    // ,��ȡ���һ��p�ڵ��,name�ڵ�
    Element name = ele.element("name");
    // 3,��ȡ���һ��name�ڵ��ֵ
    System.out.println(name.getText());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 74 with SAXReader

use of org.dom4j.io.SAXReader in project notes by KevinBlandy.

the class Dom4jDemo method add.

/**
 * ��ӽڵ����(���һ��gender��ǩ)
 */
public static void add() throws Exception {
    // ǰ�漸���趼һ��.ʡ��ע����
    Document document = new SAXReader().read(file);
    // ��ȡ���ڵ�
    Element element = document.getRootElement();
    // ��ȡҪ��ӵĽڵ����,,Ҳ���ǵڶ���p�ڵ�
    Element ele = (Element) element.elements("p").get(1);
    // ���һ������gender���ӱ�ǩ
    Element newEle = ele.addElement("gender");
    // Ϊ�±�ǩ����ı�
    newEle.setText("��");
    /**
     *ע��,��д������,��ɾ����һ��Ҫ�ǵû�д*
     */
    // �����ı���ʽ,��������
    OutputFormat format = OutputFormat.createPrettyPrint();
    // ����������
    XMLWriter wr = new XMLWriter(new FileOutputStream(file), format);
    // ���ڴ��е�Document��д���ļ���
    wr.write(document);
    // �ر���Դ
    wr.close();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 75 with SAXReader

use of org.dom4j.io.SAXReader in project notes by KevinBlandy.

the class Dom4jDemo method delete.

/**
 * ɾ���ڵ�IJ���(ɾ������һ����ӵ�gender)
 */
public static void delete() throws Exception {
    // ǰ�漸���趼һ��.ʡ��ע����
    Document document = new SAXReader().read(file);
    // ��ȡ���ڵ�
    Element element = document.getRootElement();
    // ��ȡ�ڶ���p�ڵ�
    Element ele = (Element) element.elements("p").get(1);
    // ��ȡҪɾ���Ľڵ�
    Element gen = ele.element("gender");
    // �ø��ڵ�ȥɾ��ָ���ڵ�
    ele.remove(gen);
    // �����
    write(document, file);
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document)

Aggregations

SAXReader (org.dom4j.io.SAXReader)322 Document (org.dom4j.Document)256 Element (org.dom4j.Element)195 StringReader (java.io.StringReader)120 DocumentException (org.dom4j.DocumentException)74 Test (org.junit.jupiter.api.Test)74 File (java.io.File)54 List (java.util.List)49 IOException (java.io.IOException)48 InputStream (java.io.InputStream)48 ArrayList (java.util.ArrayList)47 Node (org.dom4j.Node)28 FileInputStream (java.io.FileInputStream)25 HashMap (java.util.HashMap)24 XMLWriter (org.dom4j.io.XMLWriter)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 URL (java.net.URL)18 OutputFormat (org.dom4j.io.OutputFormat)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 Test (org.junit.Test)14