Search in sources :

Example 31 with Element

use of org.omegat.filters3.Element in project dq-easy-cloud by dq-open-cloud.

the class DqXMLUtils method getMapFromInputStream.

/**
 * <p>
 * 将xml流中的信息放入map中
 * </p>
 *
 * @param inputStream
 *            : InputStream : xml输入流
 * @param paramsMap
 *            : Map<String, Object> : xml结果容器
 * @return Map<String, Object>
 * @throws IOException
 * @author daiqi 创建时间 2018年2月23日 下午12:49:06
 */
public static Map<String, Object> getMapFromInputStream(InputStream inputStream, Map<String, Object> paramsMap) throws IOException {
    if (null == paramsMap) {
        paramsMap = new HashMap<>();
    }
    SAXBuilder builder = new SAXBuilder();
    try {
        Document doc = builder.build(inputStream);
        Element root = doc.getRootElement();
        List<Element> list = root.getChildren();
        Iterator<Element> it = list.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            Object v = DqStringUtils.EMPTY;
            List<Element> children = e.getChildren();
            if (children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = getChildren(children);
            }
            paramsMap.put(k, v);
        }
    } catch (JDOMException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return paramsMap;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 32 with Element

use of org.omegat.filters3.Element in project dq-easy-cloud by dq-open-cloud.

the class XmlUtilsTest method deleteXmlElement.

/**
 * 删除节点和属性
 * @param doc
 * @throws Exception
 */
public static void deleteXmlElement(Document doc) throws Exception {
    // 获取根元素
    Element root = doc.getRootElement();
    List<Element> personList = root.getChildren("person");
    // 循环person元素,删除person的id为1的id属性以及username子节点
    for (Element el : personList) {
        if (null != el.getAttribute("id") && "1".equals(el.getAttribute("id").getValue())) {
            el.removeAttribute("id");
            el.removeChild("username");
        }
    }
    // 循环person元素,删除person的id为2的节点
    for (int i = 0; i < personList.size(); i++) {
        Element el = personList.get(i);
        if (null != el.getAttribute("id") && "2".equals(el.getAttribute("id").getValue())) {
            // 从root节点上删除该节点
            root.removeContent(el);
        // 警告:此处删除了节点可能会使personList的长度发生变化而发生越界错误,故不能写成for(int i=0,len=personList.size(); i<len; i++)
        }
    }
    XMLOutputter out = new XMLOutputter();
    // 设置UTF-8编码,理论上来说应该不会有乱码,但是出现了乱码,故设置为GBK
    out.setFormat(Format.getCompactFormat().setEncoding("GBK"));
    // 写文件
    out.output(doc, new FileWriter("src/test.xml"));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) FileWriter(java.io.FileWriter)

Example 33 with Element

use of org.omegat.filters3.Element in project dq-easy-cloud by dq-open-cloud.

the class XmlUtilsTest method updateXmlElement.

/**
 * 更新节点
 * @param doc
 * @throws Exception
 */
public static void updateXmlElement(Document doc) throws Exception {
    // 获取根元素
    Element root = doc.getRootElement();
    // 循环person元素并修改其id属性的值
    for (Element el : root.getChildren("person")) {
        el.setAttribute("id", "haha");
    }
    // 循环设置username和password的文本值和添加属性
    for (Element el : root.getChildren()) {
        el.getChild("username").setAttribute("nameVal", "add_val").setText("update_text");
        el.getChild("password").setAttribute("passVal", "add_val").setText("update_text");
    }
    XMLOutputter out = new XMLOutputter();
    // 设置UTF-8编码,理论上来说应该不会有乱码,但是出现了乱码,故设置为GBK
    out.setFormat(Format.getCompactFormat().setEncoding("GBK"));
    // 写文件
    out.output(doc, new FileWriter("src/test.xml"));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) FileWriter(java.io.FileWriter)

Example 34 with Element

use of org.omegat.filters3.Element in project dq-easy-cloud by dq-open-cloud.

the class XmlUtilsTest method testReadXml.

@Test
public void testReadXml() {
    // 获取根元素
    Element root = doc.getRootElement();
    System.out.println("---获取第一个子节点和子节点下面的节点信息------");
    List<Element> rmElements = root.getChildren();
    Element rmElement = root.getChild("resultMap");
    System.out.println("resultMap属性的值列表" + rmElement.getChildren("result").size());
    // 第一次输入张三  第二次输出123123
    System.out.println("getContentSize:" + rmElement.getChildren("result").size());
// for (Content content : rmElement.getContent()) {
// System.out.println("content" + content);
// }
// System.out.println("result:" + rmElement.getChild("result").getAttributes());
// for(Element el: rmElement.getChildren()){
// System.out.println("attribute:" + el.getAttributes());//第一次输入张三  第二次输出123123
// }
// 
// System.out.println("---直接在根节点下就遍历所有的子节点---");
// for(Element el: root.getChildren()){
// System.out.println(el.getText());//这里输出4行空格
// System.out.println(el.getChildText("username"));//输出张三   & 1111111112
// System.out.println(el.getChildText("password"));//输出123123 &  password2
// }
}
Also used : Element(org.jdom2.Element) Test(org.junit.Test)

Example 35 with Element

use of org.omegat.filters3.Element in project archi by archimatetool.

the class SaveCanvasAsTemplateWizard method createManifest.

private String createManifest() throws IOException {
    Document doc = new Document();
    Element root = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_MANIFEST);
    doc.setRootElement(root);
    // Type
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TYPE, CanvasModelTemplate.XML_CANVAS_TEMPLATE_ATTRIBUTE_TYPE_MODEL);
    // Timestamp
    root.setAttribute(ITemplateXMLTags.XML_TEMPLATE_ATTRIBUTE_TIMESTAMP, Long.toString(System.currentTimeMillis()));
    // Name
    Element elementName = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_NAME);
    elementName.setText(fTemplateName);
    root.addContent(elementName);
    // Description
    Element elementDescription = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_DESCRIPTION);
    elementDescription.setText(fTemplateDescription);
    root.addContent(elementDescription);
    // Thumbnail
    if (fIncludeThumbnail) {
        // $NON-NLS-1$
        String keyThumb = TemplateManager.ZIP_ENTRY_THUMBNAILS + "1.png";
        Element elementKeyThumb = new Element(ITemplateXMLTags.XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL);
        elementKeyThumb.setText(keyThumb);
        root.addContent(elementKeyThumb);
    }
    return JDOMUtils.write2XMLString(doc);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document)

Aggregations

Element (org.jdom2.Element)1444 Document (org.jdom2.Document)242 Test (org.junit.Test)157 ArrayList (java.util.ArrayList)111 Attribute (org.jdom2.Attribute)106 IOException (java.io.IOException)76 File (java.io.File)71 JDOMException (org.jdom2.JDOMException)65 XMLOutputter (org.jdom2.output.XMLOutputter)54 List (java.util.List)51 SAXBuilder (org.jdom2.input.SAXBuilder)51 HashMap (java.util.HashMap)49 Test (org.junit.jupiter.api.Test)34 Map (java.util.Map)33 Node (gov.cms.qpp.conversion.model.Node)29 MCRException (org.mycore.common.MCRException)28 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)28 NamedIcon (jmri.jmrit.catalog.NamedIcon)27 Namespace (org.jdom2.Namespace)25 XmlFile (jmri.jmrit.XmlFile)24