java解析XML的包有jdom,jaxp等。你可以上sun的网站查找jaxp,这个应该是最新的xml解析工具
解决方案 »
- Java多线程始终只有一个线程能从任务队列中获得任务
- 求解决
- 做j2ee以后的发展怎么样?
- hibernate和数据库的一个超郁闷的问题
- 用url打印特定网页的源代码出问题
- js的字符问题
- 自定义标签的问题
- 关于在Winxp-SP2中weblogic81建立与MSSQLServer2000的连接池失败
- Eclipse有axis的插件吗
- JBuilder8和Weblogic7结合开发EJB遇到的一个问题!
- 一个hibernate的问题(新手)。。。。
- 哪有在hibernate 使用的po 和相应的.xml配置文件的生成器下载啊?谁知道在哪下middlegen吗?还有它的使用文档啊?急急!!!!1
import org.w3c.dom.Document;
import org.w3c.dom.*;import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; public class ReadAndPrintXMLFile{
public static void main (String argv []){
try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml")); // normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){ Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){ Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0); NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0); NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ()); }catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace (); }catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
谢谢你们的热情回答.
我的XML文档资料太复杂了,还是不太好完成.
贴点内容给你们看看.
/////////////////////////
- <cims:ClassCategory rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CIM">
<rdfs:label xml:lang="en">CIM</rdfs:label>
<rdfs:comment>The Common Information Model (CIM) represents a common logical view of EMS public data. The CIM definition consists of a comprehensive information model. This information model is largely independent of any specific technological implementation methods.</rdfs:comment>
</cims:ClassCategory>
- <rdfs:Class rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion">
<rdfs:label xml:lang="en">CimVersion</rdfs:label>
<rdfs:comment>This is the CIM version number assigned to this UML model file. The Version naming convention is to use the last major revision file name followed by the date of the last revision 9e.g., cimu08b_001212).</rdfs:comment>
<cims:profile>Nerc</cims:profile>
<cims:belongsToCategory rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#CIM" />
</rdfs:Class>
- <rdf:Property rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion.Version">
<rdfs:label xml:lang="en">Version</rdfs:label>
<rdfs:comment />
<rdfs:domain rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion" />
<cims:dataType rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#String" />
</rdf:Property>
- <cims:ClassCategory rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Core">
<rdfs:label xml:lang="en">Core</rdfs:label>
<rdfs:comment>Contains the core PowerSystemResource and ConductingEquipment entities shared by all applications plus common collections of those entities. Not all applications require all the Core entities.</rdfs:comment>
</cims:ClassCategory>
- <rdfs:Class rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Bay">
<rdfs:label xml:lang="en">Bay</rdfs:label>
<rdfs:comment>A collection of power system resources (within a given substation) including conducting equipment, protection relays, measurements, and telemetry. The typeName indicates bay classification code according to application, e.g., (OH = Overheadline) (CA = Capacitor).</rdfs:comment>
<cims:profile>Nerc</cims:profile>
<cims:belongsToCategory rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#Core" />
<rdfs:subClassOf rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#EquipmentContainer" />
</rdfs:Class>
- <rdf:Property rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Bay.bayEnergyMeasFlag">
<rdfs:label xml:lang="en">bayEnergyMeasFlag</rdfs:label>
<rdfs:comment>Indicates the presence/absence of kWh/kvarh
.....
能够解释一下jena包吗?
说详细一点,谢谢!
你可以在http://jena.sourceforge.net/找到它所有的信息。
我要解析的的确是RDF文件,
我下载了Jena-2.1.
发现用不起来.
你能指点一下吗?
谢谢!