利用Dom4j生成如下XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:3GPP:metadata:2005:mTV:BSFaddrResponse">
<BSF_ADDRESS>test address</BSF_ADDRESS>
</Root>
方法如下:
static void testGenXml() throws Exception{
String namespace = "urn:3GPP:metadata:2005:mTV:BSFaddrResponse";
String body = "";
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("Root");
Namespace namespace1 = new DefaultNamespace("xs", "http://www.w3.org/2001/XMLSchema");
Namespace namespace2 = new DefaultNamespace("", namespace);
root.add(namespace1);
root.add(namespace2);
// Void have "xmlns=""" during sub element.
Element bsfAdress = root.addElement("BSF_ADDRESS", namespace);
bsfAdress.addText("test address");
Namespace testSpace = bsfAdress.getNamespace();
System.out.println(testSpace.getStringValue());
bsfAdress.remove(namespace2);
// output with custom xml format.
OutputFormat format = new OutputFormat(" ", true);
XMLWriter writer = new XMLWriter(System.out, format);
writer.write(doc);
}
<script type="text/javascript"> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:3GPP:metadata:2005:mTV:BSFaddrResponse">
<BSF_ADDRESS>test address</BSF_ADDRESS>
</Root>
方法如下:
static void testGenXml() throws Exception{
String namespace = "urn:3GPP:metadata:2005:mTV:BSFaddrResponse";
String body = "";
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("Root");
Namespace namespace1 = new DefaultNamespace("xs", "http://www.w3.org/2001/XMLSchema");
Namespace namespace2 = new DefaultNamespace("", namespace);
root.add(namespace1);
root.add(namespace2);
// Void have "xmlns=""" during sub element.
Element bsfAdress = root.addElement("BSF_ADDRESS", namespace);
bsfAdress.addText("test address");
Namespace testSpace = bsfAdress.getNamespace();
System.out.println(testSpace.getStringValue());
bsfAdress.remove(namespace2);
// output with custom xml format.
OutputFormat format = new OutputFormat(" ", true);
XMLWriter writer = new XMLWriter(System.out, format);
writer.write(doc);
}
<script type="text/javascript"> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>