当前位置: 技术问答>java相关
请问谁用过castor和jdom?
来源: 互联网 发布时间:2015-11-15
本文导语: 用castor,具体步骤是怎样的? 我已生成了.xml文件 ,.xsd文件。还需要建立映射吗? 还有谁能具体说说jdom? | 问得太笼统,问问题要问详细的点上, 不是要别人具体地说,:) 代码: /** * xe...
用castor,具体步骤是怎样的?
我已生成了.xml文件 ,.xsd文件。还需要建立映射吗?
还有谁能具体说说jdom?
我已生成了.xml文件 ,.xsd文件。还需要建立映射吗?
还有谁能具体说说jdom?
|
问得太笼统,问问题要问详细的点上,
不是要别人具体地说,:)
代码:
/**
* xerces version 1.3.0
* xalan version 2.0.1
* jdom version 1.0beta6
* jdk version 1.2
*
* @author lulu
* @copyright com.biaoqi
*/
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Article
{
/**
* Read and parse an xml document from the file at xml/sample.xml.
* This method corresponds to the code in Listing 7.
* @return the JDOM document parsed from the file.
*/
public static Document readDocument()
{
try
{
SAXBuilder builder = new SAXBuilder();
Document anotherDocument = builder.build(new File("xml/sample.xml"));
return anotherDocument;
}
catch(JDOMException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
return null;
}
/**
* This method creates a JDOM document with elements that represent the
* properties of a car.
* This method corresponds to Listing 2.
* @return a JDOM Document that represents the properties of a car.
*/
public static Document createDocument()
{
// Create the root element
Element carElement = new Element("car");
// create the document
Document myDocument = new Document(carElement);
// add an attribute to the root element
carElement.setAttribute(new Attribute("ibs", "superibs"));
// add a comment
carElement.addContent(new Comment("Description of a car"));
// add some child elements
/*
* Note that this is the first approach to adding an element and
* textual content. The second approach is commented out.
*/
Element make = new Element("make");
make.addContent("Toyota");
carElement.addContent(make);
// carElement.addContent(new Element("make").addContent("Toyota"));
// add some more elements
carElement.addContent(new Element("model").addContent("stupid"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA"));
return myDocument;
}
/**
* This method accesses a child element of the root element of the
* document built in listing 2 with the createDocument method.
* This method corresponds to Listing 3.
* @param myDocument the JDOM document built from Listing 2
*/
public static void accessChildElement(Document myDocument)
{
// some setup
Element carElement = myDocument.getRootElement();
// Access a child element
Element yearElement = carElement.getChild("year");
// show success or failure
if(yearElement != null)
{
System.out.println("Here is the element we found: " +
yearElement.getName() + ". Its content: " +
yearElement.getText() + "n");
} else {
System.out.println("Something is wrong. We did not find a year Element");
}
}
/**
* This method removes a child element from a document. The document
* should be of the format created in Listing 2.
* This method corresponds to Listin 4.
* @param myDocument the JDOM document built from Listing 2.
*/
public static void removeChildElement(Document myDocument)
{
// some setup
System.out.println("About to remove the year element.nThe current document:");
outputDocument(myDocument);
Element carElement = myDocument.getRootElement();
// remove a child Element
boolean removed = carElement.removeChild("year");
// show success or failure
if(removed)
{
System.out.println("Here is the modified document without year:");
outputDocument(myDocument);
}
else
{
System.out.println("Something happened. We were unable to remove the year element.");
}
}
/**
* This method shows how to use XMLOutputter to output a JDOM document to
* the stdout.
* This method corresponds to Listing 5.
* @param myDocument the JDOM document built from Listing 2.
*/
public static void outputDocument(Document myDocument)
{
try
{
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(myDocument, System.out);
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
不是要别人具体地说,:)
代码:
/**
* xerces version 1.3.0
* xalan version 2.0.1
* jdom version 1.0beta6
* jdk version 1.2
*
* @author lulu
* @copyright com.biaoqi
*/
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Article
{
/**
* Read and parse an xml document from the file at xml/sample.xml.
* This method corresponds to the code in Listing 7.
* @return the JDOM document parsed from the file.
*/
public static Document readDocument()
{
try
{
SAXBuilder builder = new SAXBuilder();
Document anotherDocument = builder.build(new File("xml/sample.xml"));
return anotherDocument;
}
catch(JDOMException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
return null;
}
/**
* This method creates a JDOM document with elements that represent the
* properties of a car.
* This method corresponds to Listing 2.
* @return a JDOM Document that represents the properties of a car.
*/
public static Document createDocument()
{
// Create the root element
Element carElement = new Element("car");
// create the document
Document myDocument = new Document(carElement);
// add an attribute to the root element
carElement.setAttribute(new Attribute("ibs", "superibs"));
// add a comment
carElement.addContent(new Comment("Description of a car"));
// add some child elements
/*
* Note that this is the first approach to adding an element and
* textual content. The second approach is commented out.
*/
Element make = new Element("make");
make.addContent("Toyota");
carElement.addContent(make);
// carElement.addContent(new Element("make").addContent("Toyota"));
// add some more elements
carElement.addContent(new Element("model").addContent("stupid"));
carElement.addContent(new Element("year").addContent("1997"));
carElement.addContent(new Element("color").addContent("green"));
carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA"));
return myDocument;
}
/**
* This method accesses a child element of the root element of the
* document built in listing 2 with the createDocument method.
* This method corresponds to Listing 3.
* @param myDocument the JDOM document built from Listing 2
*/
public static void accessChildElement(Document myDocument)
{
// some setup
Element carElement = myDocument.getRootElement();
// Access a child element
Element yearElement = carElement.getChild("year");
// show success or failure
if(yearElement != null)
{
System.out.println("Here is the element we found: " +
yearElement.getName() + ". Its content: " +
yearElement.getText() + "n");
} else {
System.out.println("Something is wrong. We did not find a year Element");
}
}
/**
* This method removes a child element from a document. The document
* should be of the format created in Listing 2.
* This method corresponds to Listin 4.
* @param myDocument the JDOM document built from Listing 2.
*/
public static void removeChildElement(Document myDocument)
{
// some setup
System.out.println("About to remove the year element.nThe current document:");
outputDocument(myDocument);
Element carElement = myDocument.getRootElement();
// remove a child Element
boolean removed = carElement.removeChild("year");
// show success or failure
if(removed)
{
System.out.println("Here is the modified document without year:");
outputDocument(myDocument);
}
else
{
System.out.println("Something happened. We were unable to remove the year element.");
}
}
/**
* This method shows how to use XMLOutputter to output a JDOM document to
* the stdout.
* This method corresponds to Listing 5.
* @param myDocument the JDOM document built from Listing 2.
*/
public static void outputDocument(Document myDocument)
{
try
{
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(myDocument, System.out);
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
|
castor什么问题?要例程吗?