JDM use details

zhaozj2021-02-16  33

BROMON original copyright reprint, please specify

Jdom is a good API, calculating simple and efficient, the most important thing is that it has become part of JCP, this is to get. People who write documents on www.jdom.org are too lazy, the document is surprisingly, the most widely passed the "JDOM to make Java XML easy", but this article involves basic Reading and writing, far from being able to do actually work. It took two days to finish the basic operation of JDOM, covering most of the operations: elements, attributes, namespaces, PI, DTD, Schema, and the general application have no problem. Anyway, I didn't see more detailed versions online. If you have seen it, please leave it. Temporarily not writing a detailed description, let's post a few procedures, it is enough for experienced Java developers. The programs have been tested, and the JDom I use is a version 0.9.

1. Create an XML document: import java.io. *; import org.jdom. *; Import org.jdom.input. *; Import org.jdom.output. *;

public class CreateXML {public void Create () {try {Document doc = new Document (); ProcessingInstruction pi = new ProcessingInstruction ( "xml-stylesheet", "type =" text / xsl "href =" test.xsl ""); Doc.addContent (PI); namespace ns = namespace.getNamespace ("http://xml.org/sax/features/validation"; true); Document Doc = Builder.Build (New FileReader ("Author.xml") ); System.out.println (""); xmloutputter outper = new xmloutputTer (); outputter.output (doc, system.out);} catch (exception e) {system.out.println (e);} } Public static void main (string args []) {new xmlwithdtd (). Validate ();}}

It should be noted that this program does not specify which DTD file used. The location of the DTD file is specified in XML, and the DTD does not support namespace. One XML can only reference a DTD, so the program directly reads the DTD specified in XML, and the program itself is not specified. But then, I can only use external DTD reference methods? High people pointing.

3, XML Schema Verified: Import Java.io. *; Import Org.jdom. *; Import Org.jDom.Input. *; Import Org.jDom.Output. *;

public class XMLWithSchema {String xml = "test.xml"; String schema = "test-schema.xml"; public void validate () {try {SAXBuilder builder = new SAXBuilder (true); // specified constraint way XML schema builder .setfeature ("http://apache.org/xml/features/Validation/schema;" http://apache.org/xternal); "http://apache.org/Xml/properties/schema/external -noNamespaceSchemaLocation ";, schema); Document doc = builder.build (new FileReader (xml)); System.out.println (" fixed up "); XMLOutputter outputter = new XMLOutputter (); outputter.output (doc, System. OUT;} catch (Exception E) {system.out.println ("Verification Failure:" E);}}} The above program pointed out the location of the XML Schema file to be introduced.

The system default output is UTF-8, which may result in garbled.

The skill of reading XML documents is already flooding online, and there is no list here.

转载请注明原文地址:https://www.9cbs.com/read-21743.html

New Post(0)