如何在Java DOM XML构建器中添加名称空间前缀

时间:2015-11-11 17:27:15

标签: java xml dom

我需要在我的xml中设置元素的前缀。我需要以下面的格式打印xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cars xmlns:dcterms="http://purl.org/dc/terms/" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<supercars company="Ferrari">
<dcterms:carname type="formula one">Ferrari 101</dcterms:carname>
<msxsl:carname type="sports">Ferrari 202</msxsl:carname>
</supercars>
</cars>

但是,我无法在XML中添加前缀。我得到的就是这个。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cars xmlns:dcterms="http://purl.org/dc/terms/" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<supercars company="Ferrari">
<carname type="formula one">Ferrari 101</carname>
<carname type="sports">Ferrari 202</carname>
</supercars>
</cars>

以下是我的Java代码:

public static void main(String args[]) {

      try {
         DocumentBuilderFactory dbFactory =
         DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = 
            dbFactory.newDocumentBuilder();

         Document doc = dBuilder.newDocument();

         // root element

         Element rootElement = doc.createElement("cars");
         rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:dcterms", "http://purl.org/dc/terms/");
         rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:msxsl", "urn:schemas-microsoft-com:xslt");
         doc.appendChild(rootElement);


         //  supercars element
         Element supercar = doc.createElement("supercars");

         rootElement.appendChild(supercar);

         // setting attribute to element
         Attr attr = doc.createAttribute("company");
         attr.setValue("Ferrari");
       //  supercar.setAttributeNodeNS(attr);
         supercar.setAttributeNode(attr);

         // carname element
         Element carname = doc.createElement("carname");
         Attr attrType = doc.createAttribute("type");
         attrType.setValue("formula one");
         carname.setAttributeNode(attrType);
         carname.appendChild(
         doc.createTextNode("Ferrari 101"));
         supercar.appendChild(carname);

         Element carname1 = doc.createElement("carname");
         Attr attrType1 = doc.createAttribute("type");
         attrType1.setValue("sports");
         carname1.setAttributeNode(attrType1);
         carname1.appendChild(
         doc.createTextNode("Ferrari 202"));
         supercar.appendChild(carname1);

         // write the content into xml file
         TransformerFactory transformerFactory =
         TransformerFactory.newInstance();
         Transformer transformer =
         transformerFactory.newTransformer();
         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
         DOMSource source = new DOMSource(doc);
         StreamResult result =
         new StreamResult(new File("cars.xml"));
         transformer.transform(source, result);
         // Output to console for testing
         StreamResult consoleResult =
         new StreamResult(System.out);
         transformer.transform(source, consoleResult);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}

我查看了这个链接Adding namespace prefix XML String using XML DOM。但是,这只谈到了SAX做事的方式。我需要在DOM中执行此操作。

我尝试了setPrefix方法,它给我带来了NAMESPACE_ERR

1 个答案:

答案 0 :(得分:3)

首先,如果您想使用支持命名空间的DOM,请确保设置dbFactory.setNamespaceAware(true);

然后使用createElementNS,例如Element carname = doc.createElementNS("http://purl.org/dc/terms/", "dcterms:carname");,用于在命名空间中创建元素。对另一个命名空间中的元素使用相同的方法。