使用Java进行XML解析,获取元素值和属性值 [英] XML parsing using Java with getting element values and attribute values
问题描述
我有一个XML文件,元素也有属性。
我有一个简单的java文件,它解析和打印文本文件中元素的值,但不是元素属性值。
请帮助您获取要打印的属性值。
我粘贴下面的代码:
-------- employees.xml文件-----------
<?xml version =1.0encoding =UTF-8?>
< Personnel>
<员工类型=永久>
< Name> Seagull< / Name>
< Id> 3674< / Id>
< Age> 34< / Age>
< / Employee>
< Employee type =contract>
< Name> Robin< / Name>
< Id> 3675< / Id>
< Age> 25< / Age>
< / Employee>
<员工类型=永久>
< Name> Crow< / Name>
< Id> 3676< / Id>
< Age> 28< / Age>
< / Employee>
< / Personnel>
--------------------- ------- StoreData.java ---------------------------------------- -
import java.io. *;
import org.w3c.dom。*;
import org.xml.sax。*;
import javax.xml.parsers。*;
import javax.xml.transform。*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
公共类StoreData {
static public void main(String [] arg){
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in) );
System.out.print(输入XML文件名:);
String xmlFile = bf.readLine();
文件file = new File(xmlFile);
if(file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
//创建变换器
Transformer tFormer = TransformerFactory.newInstance()。newTransformer();
//输出类型(text / xml / html)
tFormer.setOutputProperty(OutputKeys.METHOD,text);
//将文档写入文件
源source = new DOMSource(doc);
//创建文件以查看xml数据为(vk.txt / vk.doc / vk.xls / vk.shtml / vk.html)
结果result = new StreamResult(new file(file 。文本));
tFormer.transform(来源,结果);
System.out.println(文件创建成功!);
}
else {
System.out.println(找不到文件!);
}
}
catch(例外e){
System.err.println(e);
System.exit(0);
}
}}
你正在使用的是一个XSLT转换,它默认转换为元素文本。
使用相同的技术,需要一个拥有样式表:
InputStream xsltIn = StoreData.class.getResourceAsStream(/ employees.xslt);
StreamSource xslt = new StreamSource(xsltIn);
/// StreamSource xslt = new StreamSource(... / employees.xslt);
Transformer tFormer = TransformerFactory.newInstance()。newTransformer(xslt);
上面我使用的是资源,而不是文件系统文件,因此它与应用程序一起打包。 / p>
employees.xslt:
<?xml version =1.0 encoding =UTF-8?>
< xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:template match =/ Personnel>
人员:
< xsl:apply-templates select =./*/>
结束人员。
< / xsl:template>
< xsl:template match =Employee>
*员工:< xsl:apply-templates select =./@* | ./*\"/>
< / xsl:template>
< xsl:template match =Name | Id | Age | @type>
- < xsl:value-of select =name()/>:< xsl:value-of select =。/>
< / xsl:template>
< / xsl:stylesheet>
哪会产生:
人员:
*员工:
- 类型:永久
- 名称:Seagull
- Id:3674
- 年龄:34
*员工:
- 类型:合约
- 姓名:Robin
- Id:3675
- 年龄:25
*员工:
- 类型:永久
- 名称:Crow
- Id:3676
- 年龄:28
结束人员。
i have an XML file and the elements also have attributes. I have a simple java file which is parsing and printing values of elements in a text file but not element attribute values. Please can you help in getting the attributes values also to be printed. I am pasting the code below: --------employees.xml file-----------
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
----------------------------StoreData.java-----------------------------------------
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class StoreData{
static public void main(String[] arg) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
//Create transformer
Transformer tFormer = TransformerFactory.newInstance().newTransformer();
//Output Types (text/xml/html)
tFormer.setOutputProperty(OutputKeys.METHOD, "text");
// Write the document to a file
Source source = new DOMSource(doc);
// Create File to view your xml data as (vk.txt/vk.doc/vk.xls/vk.shtml/vk.html)
Result result = new StreamResult(new File("file.txt"));
tFormer.transform(source, result);
System.out.println("File creation successfully!");
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.err.println(e);
System.exit(0);
}
} }
What you are using is an XSLT transformation which by default transforms to element texts.
Using the same technique, one needs an own "stylesheet":
InputStream xsltIn = StoreData.class.getResourceAsStream("/employees.xslt");
StreamSource xslt = new StreamSource(xsltIn);
///StreamSource xslt = new StreamSource(".../employees.xslt");
Transformer tFormer = TransformerFactory.newInstance().newTransformer(xslt);
Above I used a resource, not a file system File, so it is packed with the application.
employees.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Personnel">
Personnel:
<xsl:apply-templates select="./*"/>
End personnel.
</xsl:template>
<xsl:template match="Employee">
* Employee:<xsl:apply-templates select="./@*|./*"/>
</xsl:template>
<xsl:template match="Name|Id|Age|@type">
- <xsl:value-of select="name()"/>: <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Which would produce:
Personnel:
* Employee:
- type: permanent
- Name: Seagull
- Id: 3674
- Age: 34
* Employee:
- type: contract
- Name: Robin
- Id: 3675
- Age: 25
* Employee:
- type: permanent
- Name: Crow
- Id: 3676
- Age: 28
End personnel.
这篇关于使用Java进行XML解析,获取元素值和属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!