jdom解析xml

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.jdom.CDATA;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;
import org.jdom.xpath.XPath;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class JDOMTest {

	public JDOMTest() throws IOException, JDOMException, TransformerException {
		Document doc = createDoc();
		printDoc(doc);
		
		doc = load("address.xml");
		
		findPostCode(doc);
		
		printDoc(doc);
		
		doc = load("class.xml", getClass().getResource("/net/xml/class.dtd"));
		printDoc(doc);
	
		doc = load("booklist.xml");
		printDoc(doc);
		doc = transform(doc, "booklist2books.xslt");
		printDoc(doc);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 
		try {
			new JDOMTest();
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	
	public Document createDoc(){
		Document doc = new Document();
		
		doc.addContent(new Comment("测试注释信息"));
		
		Element address = new Element("address");
		doc.addContent(address);
		
		address.addContent(
				new Element("name").addContent(
						new Element("title").addContent(
								new Text("<b>Mr.</b>")
						)
				).addContent(
						new Element("first-name").addContent(
								new CDATA("<b>Robin</b>")
						)
				
				)
		);
		
		return doc;
		
	}
	
	public void printDoc(Document doc) throws IOException{
		Format format = Format.getPrettyFormat();
		XMLOutputter outputter = new XMLOutputter(format);
		
		outputter.output(doc, new OutputStreamWriter(System.out));
		
		
	}
	
	public Document load(String xmlFile) throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File(xmlFile));
		
		return doc;
	}

	
	public Document load(String xmlFile, final URL dtd) throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder(true);
		
		builder.setEntityResolver(new EntityResolver(){

			public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
				
				if("//test By Robin".equals(publicId)){
					
					return new InputSource(dtd.openStream());
					
				}
				
				return new InputSource(systemId);
			}
		});
		
		Document doc = builder.build(new File(xmlFile));
		
		return doc;
	}
	
	public Document transform(Document doc, String xsltFile) throws FileNotFoundException, TransformerException{
		
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer(new StreamSource(new FileInputStream(xsltFile)));
	
		JDOMSource source = new JDOMSource(doc);
		JDOMResult result = new JDOMResult();
		
		transformer.transform(source, result);
		
		return result.getDocument();
		
	}

	public void findPostCode(Document doc) throws JDOMException{
		
		XPath path = XPath.newInstance("/address/post-code");
		
		String code = path.valueOf(doc);
		
		System.out.println(code);
		
		
		System.out.println(doc.getRootElement().getChildText("post-code"));
	}
}

 

你可能感兴趣的:(java,.net,xml)