java中的xml文档存在问题

java中的xml文档存在问题,java,xml,Java,Xml,我正在编写一个简单的程序,获取一个字符串并将其转换为xml文档,但它不显示内容的值,我将其设置为show null import org.w3c.dom.Document; import org.w3c.dom.Element; public class Server { /** * @param args */ public static void main(String[] args) {

我正在编写一个简单的程序,获取一个字符串并将其转换为xml文档,但它不显示内容的值,我将其设置为show null

import org.w3c.dom.Document;
import org.w3c.dom.Element;


    public class Server {

        /**
         * @param args
         */
        public static void main(String[] args) {
            Server server=new Server();
            Document dc=server.stringToDocument("f0");
            System.out.println(dc.getTextContent());

        }
        public org.w3c.dom.Document stringToDocument(String order)
        {
            org.w3c.dom.Document result=null;
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            try{
            DocumentBuilder db=dbf.newDocumentBuilder();
            result=db.newDocument();
            Element el=result.createElement("ORDER");
            el.setTextContent(order);
            }
            catch (Exception e) {
                System.out.println("DB in line 1418 exception");
            }



            return result;
        }

    }

您需要将元素添加到文档中,以使其与文档相关联。尝试添加行:

result.appendChild(el);
result.appendChild(el);

有关详细信息,请参见。

您需要将元素添加到文档中,以便将其与文档关联。尝试添加行:

result.appendChild(el);
result.appendChild(el);

有关更多信息,请参阅。

您需要添加行:

result.appendChild(el);
result.appendChild(el);
到stringToDocument方法

要获取要打印的xml字符串,可以使用以下方法:

public String documentToString(Document doc) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        return xmlString;
    } catch (Exception e) {
        return null;
    }
}

您需要添加以下行:

result.appendChild(el);
result.appendChild(el);
到stringToDocument方法

要获取要打印的xml字符串,可以使用以下方法:

public String documentToString(Document doc) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        String xmlString = result.getWriter().toString();
        return xmlString;
    } catch (Exception e) {
        return null;
    }
}

您正在文档节点上调用
getTextContent
。这通常会导致一个空值,您可以在api文档中读取:

()


如果调用
dc.getFirstChild().getTextContent()
(在将元素附加到文档之后),您将获得该值,因为现在您在元素节点上调用
getTextContent

在文档节点上调用
getTextContent
。这通常会导致一个空值,您可以在api文档中读取:

()


如果调用
dc.getFirstChild().getTextContent()
(在将元素附加到文档之后),您将获得该值,因为现在你在一个元素节点上调用
getTextContent

我这样做,但没有变化。其他想法AAAA?我这样做,但没有变化。其他想法AAAA?我这样做,现在就可以了。非常感谢。我接受了你的回答。我这样做,现在就可以了。非常感谢。我接受了你的答案