如何使用Java在xml中为属性设置前缀

时间:2018-06-20 12:13:33

标签: java xml xml-parsing domparser

我喜欢在使用XML创建Java文件时为属性设置前缀。 我需要的结果是

<entry gd:etag="*">
    <id>Text</id>
</entry>

但是我得到了

<entry etag="*">
    <id>Text</id>
</entry>

我写的代码是

DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuildFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element entry = doc.createElement("entry");
entry.setAttribute("etag","*");
doc.appendChild(entry);
Element id = doc.createElement("id");
id.setTextContent("Text");
entry.appendChild(id);
.....

我不知道如何为属性设置前缀。有人可以建议我该怎么做吗?

2 个答案:

答案 0 :(得分:2)

您需要将DocumentBuilderFactory设置为可识别名称空间,并且需要为该前缀设置名称空间,并将setAttributeNS用于具有XML名称空间的属性。

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
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;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Entry {
    public static void main(String[] args)
            throws IOException, ParserConfigurationException, TransformerException {
        String ns = "http://schemas.google.com/g/2005"; // just a guess
        DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
        docBuildFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuildFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element entry = doc.createElement("entry");
        entry.setAttributeNS(ns, "gd:etag", "*");
        doc.appendChild(entry);
        Element id = doc.createElement("id");
        id.setTextContent("Text");
        entry.appendChild(id);
        printDocument(doc, System.out);
    }

    public static void printDocument(Document doc, OutputStream out)
            throws IOException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }
}

输出:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*">
    <id>Text</id>
</entry>

用正确的名称空间替换名称空间URN;我只是在谷歌搜索gd:etag前缀的基础上猜出来的,它带来了一些Google Data API和Google Contacts API的东西。

答案 1 :(得分:0)

SimpleXml可以做到:

final SimpleXml simple = new SimpleXml();
final Element element =
    element("entry").attribute("gd:etag", "*")
        .child(element("id").text("Text"));
System.out.println(simple.domToXml(element));

将输出:

<entry gd:etag="*"><id>Text</id></entry>

从Maven Central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>