1.SAX解析名称空间:继承DefaultHandler,复写startPrefixMapping()和endPrefixMapping()两个方法解析名称空间。
代码如下:
`
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SAXNSPrinter extends DefaultHandler
{
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException
{
System.out.println("----------------Namespace scope behin--------------");
System.out.println("Namespace prefix : "+prefix + "\nNamespace uri : "+uri +"\n\n");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXException
{
if(!uri.equals(""))//必须为空,与DOM不一样
{
System.out.println("Element name : "+qName);
System.out.println("Local name : "+localName);
int index = qName.indexOf(":");
if(index > 0)//有名称空间
{
System.out.println("Namespace prefix : " + qName.substring(0,index));
}
System.out.println("Namespace uri : "+uri);
System.out.println();
}
int len = attrs.getLength();
for(int i=0;i<len;i++)
{
String attrUri = attrs.getURI(i);
if(!uri.equals(""))
{
System.out.println("Attribute name : "+attrs.getQName(i));
System.out.println("Local name : "+attrs.getLocalName(i));
int index = attrs.getQName(i).indexOf(":");
if(index > 0)//有名称空间
{
System.out.println("Namespace prefix : " + attrs.getQName(i).substring(0,index));
}
System.out.println("Namespace uri : "+attrUri);
System.out.println();
}
}
}
@Override
public void endPrefixMapping(String prefix) throws SAXException
{
System.out.println("Namespace prefix : " + prefix);
System.out.println("----------------Namespace scope end--------------\n");
}
public static void main(String[] args) throws Exception
{
/*
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);//设置对名称空间解析的支持,必须的
SAXParser sp = spf.newSAXParser();
sp.parse("books.xml", new SAXNSPrinter());
*/
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(new SAXNSPrinter());
xmlReader.parse(new InputSource(new FileInputStream("books.xml")));
}
}
2、DOM解析名称空间:通过getXXX获取信息。
代码如下:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
public class DomNSPrinter
{
//打印元素,属性的名称空间信息
public static void printNSInfo(Node node)
{
short type = node.getNodeType();
switch(type)
{
case Node.ELEMENT_NODE:
if(node.getNamespaceURI() != null)
{
System.out.println("Element name : "+node.getNodeName());
System.out.println("Local name : " +node.getLocalName());
System.out.println("Namespace Prefix : "+node.getPrefix());
System.out.println("Namespace URI : "+node.getNamespaceURI());
System.out.println();
}
if(node.hasAttributes()){
NamedNodeMap attrs = node.getAttributes();
int len = attrs.getLength();
for(int i=0;i<len;i++)
{
Node attr = attrs.item(i);
if(attr.getNamespaceURI() != null)
printNSInfo(attr);//递归调用
}
}
Node child = node.getFirstChild();
while(child!=null){
printNSInfo(child);
child = child.getNextSibling();//下一个节点
}
break;
case Node.ATTRIBUTE_NODE:
System.out.println("Atrribute name : "+node.getNodeName());
System.out.println("Local name : " +node.getLocalName());
System.out.println("Namespace Prefix : "+node.getPrefix());
System.out.println("Namespace URI : "+node.getNamespaceURI());
System.out.println();
break;
default :
break;
}
}
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);//对名称空间的解析。必须有
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("books.xml"));
printNSInfo(doc.getDocumentElement());
}
}
3、JDOM解析名称空间:通过getAdditionalNamespaces();获取元素上附加的其他名称空间声明(本元素未使用的名称空间声明),然后通过Iterater遍历。
代码如下:
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
public class JDOMNSPrinter
{
public static void printNSInfo(Element elt){
String eltNSUri = elt.getNamespaceURI();
if(!eltNSUri.equals(""))
{
System.out.println("Element name : "+elt.getQualifiedName());
System.out.println("Local name : "+elt.getName());
System.out.println("Namespace prefix : "+elt.getNamespacePrefix());
System.out.println("Namespace uri : "+eltNSUri);
System.out.println();
}
List l = elt.getAdditionalNamespaces();
for (Iterator iterator = l.iterator(); iterator.hasNext();)
{
Namespace ns = (Namespace) iterator.next();
System.out.println("------------------Namespace declaration------------");
System.out.println("Namespace prefix : "+ns.getPrefix());
System.out.println("Namespace uri : "+ns.getURI());
System.out.println();
}
l = elt.getAttributes();
for (Iterator iterator = l.iterator(); iterator.hasNext();)
{
Attribute attr = (Attribute) iterator.next();
String attrNSUri = attr.getNamespaceURI();
if(!attrNSUri.equals(""))
{
System.out.println("Element name : "+attr.getQualifiedName());
System.out.println("Local name : "+attr.getName());
System.out.println("Namespace prefix : "+attr.getNamespacePrefix());
System.out.println("Namespace uri : "+attr);
System.out.println();
}
}
l = elt.getChildren();
for (Iterator iterator = l.iterator(); iterator.hasNext();)
{
Element element = (Element) iterator.next();
printNSInfo(element);
}
}
public static void main(String[] args) throws Exception
{
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(new File("books.xml"));
printNSInfo(doc.getRootElement());
}
}
4、DOM4J解析名称空间:采用访问者VisitorSupport内部类实现解析,与JDOM类似,通过getAdditionalNamespaces();获取元素上附加的其他名称空间声明(本元素未使用的名称空间声明),然后通过Iterater遍历。
代码如下:
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
public class DOM4jNSPrinter
{
public static void main(String[] args) throws Exception
{
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("books.xml"));
doc.accept(new VisitorPrinter());
}
private static class VisitorPrinter extends VisitorSupport
{
@Override
public void visit(Attribute attr)
{
String attrNSuri = attr.getNamespaceURI();
if(!attrNSuri.equals(""))
{
System.out.println("Attribute name : "+attr.getQualifiedName());
System.out.println("Local name : "+attr.getName());
System.out.println("Namespace prefix : "+attr.getNamespacePrefix());
System.out.println("Namespace uri : "+attrNSuri);
System.out.println();
}
}
@Override
public void visit(Element elt)
{
String eltNSUri = elt.getNamespaceURI();
if(!eltNSUri.equals(""))
{
System.out.println("Element name : "+elt.getQualifiedName());
System.out.println("Local name : "+elt.getName());
System.out.println("Namespace prefix : "+elt.getNamespacePrefix());
System.out.println("Namespace uri : "+eltNSUri);
System.out.println();
}
List l = elt.additionalNamespaces();//与JDOM类似
for (Iterator iterator = l.iterator(); iterator.hasNext();)
{
Namespace ns = (Namespace) iterator.next();
System.out.println("------------------Namespace declaration------------");
System.out.println("Namespace prefix : "+ns.getPrefix());
System.out.println("Namespace uri : "+ns.getURI());
System.out.println();
}
}
}
}
5、books.xml代码如下:
<?xml version ="1.0" encoding = "gb2312"?>
<bks:books xmlns:bks="http://www.qinyi/books"
xmlns:text="http://www.qinyi/text">
<bks:book bks:bookID = "book001">
<bks:title>JAVA web开发详解</bks:title>
<bks:author xmlns="http://www.qinyi/people">
<name>丽丽</name>
<title>教师</title>
</bks:author>
</bks:book>
</bks:books>
`