• JAVA读取XML并打印


    在G盘下新建XML文档:person.xml,XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    
    <students> 
      <student id="1"> 
        <name>a</name>  
        <sex></sex>  
        <age>18</age> 
      </student>  
      <student id="2"> 
        <name>b</name>  
        <sex></sex>  
        <age>16</age> 
      </student> 
    </students>

     读取XML文件并打印,代码如下:

    package jichu;
    
    import java.io.StringWriter;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    
    public class XmlPrinter {
        public static String xmlPrintFromFileName(String FileName) {
            String xml = "";
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(FileName);
                Source oldData = new DOMSource(document);
                StreamResult newData = new StreamResult(new StringWriter());
                Transformer transformer = TransformerFactory.newInstance()
                        .newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "2");
                transformer.transform(oldData, newData);
                xml = newData.getWriter().toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return xml;
        }
    }

    测试代码:

    package jichu;
    
    public class MainClass {
        public static void main(String[] args) {
            String xml = XmlPrinter.xmlPrintFromFileName("G:\person.xml");
            System.out.println(xml);
        }
    }

    打印:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <students> 
      <student id="1"> 
        <name>a</name>  
        <sex></sex>  
        <age>18</age> 
      </student>  
      <student id="2"> 
        <name>b</name>  
        <sex></sex>  
        <age>16</age> 
      </student> 
    </students>

    读取XML字符串,格式化后打印,代码如下:

    package jichu;
    
    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    public class XmlPrinter {
        public static String xmlPrintFromString(String xmlString) {
            String xml = "";
            try {
                StringReader reader = new StringReader(xmlString);
                Source oldData = new StreamSource(reader);
                StreamResult newData = new StreamResult(new StringWriter());
                Transformer transformer = TransformerFactory.newInstance()
                        .newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "2");
                transformer.transform(oldData, newData);
                xml = newData.getWriter().toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return xml;
        }
    }

    测试:

    package jichu;
    
    public class MainClass {
        public static void main(String[] args) {
            String xml = "<a><b></b><c/><d>text</d></a>";
            System.out.println(XmlPrinter.xmlPrintFromString(xml));
        }
    }

    打印:

    <?xml version="1.0" encoding="UTF-8"?>
    <a>
      <b/>
      <c/>
      <d>text</d>
    </a>
  • 相关阅读:
    C#设置窗体最大化且不遮挡任务栏的方法
    C# Base64解码 二进制输出
    导出Excel并下载,但无法定制样式的方法!
    C# List 转Datatable
    查询sql语句耗时的方法!
    301跳转
    文章关键字加链接
    文本框样式默认文本
    JForum二次开发(一)
    MongoDB 学习笔记(三)—— 修改器的使用
  • 原文地址:https://www.cnblogs.com/SQP51312/p/6527161.html
Copyright © 2020-2023  润新知