完整程序:
package jdom;
import java.io.File;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
public class JDOM
{
private static StringBuffer strbuf = new StringBuffer();
/**
* main
*
* @param argv String
*/
public static void main(String[] argv)
{
if(argv.length == 0 ¦ ¦ (argv.length == 1 && argv[0].equals("-help")))
{
System.out.println("Usage:java JDOM uri");
System.exit(1);
}
try
{
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new File(argv[0]));
Element root = doc.getRootElement();
//System.out.println(doc.getBaseURI());
strbuf.append("<?xml version=/"1.0/" encoding=/"GB2312/"?>").append(System.getProperty("line.separator"));
strbuf.append("<");
strbuf.append(root.getName());
if(!"".equals(root.getNamespaceURI())) //Namespace信息
{
strbuf.append(" xmlns=/"" + root.getNamespaceURI() + "/"");
}
//得到附加的Namespace,比如:xmlns:xsi
accessAdditionalNamespaces(root);
//得到附加的Namespace的属性,如:xsi:schemaLocation
accessAttribute(root);
strbuf.append(">");
//遍历节点!
accessElement(root);
strbuf.append("</" + root.getName() + ">");
System.out.println(strbuf.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* accessElement
* 遍历结点
* @param parent Element
*/
public static void accessElement(Element parent)
{
List listChild = parent.getChildren();
int iChild = listChild.size();
for(int i = 0;i < iChild;i++)
{
Element e = (Element)listChild.get(i);
strbuf.append(System.getProperty("line.separator"));
strbuf.append("<");
strbuf.append(e.getName());
//访问属性
accessAttribute(e);
strbuf.append(">");
strbuf.append(e.getTextTrim());
//递归访问节点
accessElement(e);
strbuf.append("</");
strbuf.append(e.getName());
strbuf.append(">");
}
//恰当换行成XML原有的格式
if(iChild > 0)
{
strbuf.append(System.getProperty("line.separator"));
}
}
/**
* accessAttribute
* 访问结点的属性
* @param e Element
*/
public static void accessAttribute(Element e)
{
List listAttributes = e.getAttributes();
int iAttributes = listAttributes.size();
for(int j = 0;j < iAttributes;j++)
{
Attribute attribute = (Attribute)listAttributes.get(j);
//问题:属性带有命名空间xsi:的怎么处理?
//<name xsi:type="Full">YuLimin</name>
// + attribute.getNamespacePrefix() + ":"
strbuf.append(" ");
String strNS = attribute.getNamespace().getPrefix();
if(!"".equals(strNS))
{
strbuf.append(strNS).append(":");
}
strbuf.append(attribute.getName() + "=/"" + attribute.getValue() + "/"");
}
}
/**
* accessAdditionalNamespaces
* 访问附加的命名空间
* @param e Element
*/
public static void accessAdditionalNamespaces(Element e)
{
List listAdditionalNamespaces = e.getAdditionalNamespaces();
int iAttributes = listAdditionalNamespaces.size();
for(int j = 0;j < iAttributes;j++)
{
Namespace namespace = (Namespace)listAdditionalNamespaces.get(j);
strbuf.append(" xmlns:" + namespace.getPrefix() + "=/"" + namespace.getURI() + "/"");
}
}
}
XML文件内容
<?xml version="1.0" encoding="GB2312"?>
<specificationSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.citi.qut.edu.au/yawl YAWL_Schema.xsd" xmlns="http://www.citi.qut.edu.au/yawl" xmlns:mm="www.citi.qut.edu.au/yawl/exampleSchemas/make_trip">
<specification uri="Maketrip1.ywl">
<metaData/>
<decomposition id="pay" xsi:type="问题在这里!如何处理带有命名空间的属性?">
<inputParam name="customer">
<type>xs:string</type>
</inputParam>
<inputParam name="payment_account_number">
<type>xs:string</type>
</inputParam>
<inputParam name="carDetails">
<type>xs:string</type>
</inputParam>
<inputParam name="hotelDetails">
<type>xs:string</type>
</inputParam>
<inputParam name="flightDetails">
<type>xs:string</type>
</inputParam>
</decomposition>
</specification>
</specificationSet>
2. Element类的getContent()方法返回一个List对象,它包括了一个元素的所有内容:注释、属性、处理指令、文本和子元素。利用它我们可以遍历XML文档。下面的程序来自《java语言与xml教程》
</HD>
原文链接:http://blog.csdn.net/it_man/article/details/757864