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>

 

posted @ 2017-03-10 09:08  SQP51312  阅读(2665)  评论(0编辑  收藏  举报