如何在Java中将XML片段转换为字符串

时间:2023-02-08 21:47:52

Input XML

<Employees>
    <Employee>
        <EmpId>101398</EmpId>
        <Name>ABC</Name>
    </Employee>
    <Employee>
        <EmpId>101399</EmpId>
        <Name>XYZ</Name>
    </Employee>
<Employees>


Output XML
String : "<Employee><EmpId>101398</EmpId><Name>ABC</Name></Employee>"

Note: I do not want to use XSLT. I would be thankful for your answer.

注意:我不想使用XSLT。我很感谢你的回答。

1 个解决方案

#1


0  

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



public class XMLFormatter {
    public static void main(String[] args) throws Exception{
        File fXmlFile = new File("src/a.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("Employee");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            System.out.println(nodeToString(nList.item(temp)));

        }
    }

    private static String nodeToString(Node node) {
        StringWriter sw = new StringWriter();
        try {
          Transformer t = TransformerFactory.newInstance().newTransformer();
          t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
          t.setOutputProperty(OutputKeys.INDENT, "yes");
          t.transform(new DOMSource(node), new StreamResult(sw));
        } catch (TransformerException te) {
          System.out.println("nodeToString Transformer Exception");
        }
        return sw.toString();
      }


}

The Running code for your problem a.xml contains your xml deatils

您的问题的运行代码a.xml包含您的xml deatils

#1


0  

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



public class XMLFormatter {
    public static void main(String[] args) throws Exception{
        File fXmlFile = new File("src/a.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("Employee");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            System.out.println(nodeToString(nList.item(temp)));

        }
    }

    private static String nodeToString(Node node) {
        StringWriter sw = new StringWriter();
        try {
          Transformer t = TransformerFactory.newInstance().newTransformer();
          t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
          t.setOutputProperty(OutputKeys.INDENT, "yes");
          t.transform(new DOMSource(node), new StreamResult(sw));
        } catch (TransformerException te) {
          System.out.println("nodeToString Transformer Exception");
        }
        return sw.toString();
      }


}

The Running code for your problem a.xml contains your xml deatils

您的问题的运行代码a.xml包含您的xml deatils