使用Java进行XML解析,获取元素值和属性值
我有一个XML文件,元素也有属性。 我有一个简单的java文件,它解析并打印文本文件中元素的值,但不打印元素属性值。 请您帮助获取要打印的属性值。 我正在粘贴下面的代码: --------employees.xml文件-----------使用Java进行XML解析,获取元素值和属性值,java,xml,parsing,Java,Xml,Parsing,我有一个XML文件,元素也有属性。 我有一个简单的java文件,它解析并打印文本文件中元素的值,但不打印元素属性值。 请您帮助获取要打印的属性值。 我正在粘贴下面的代码: --------employees.xml文件----------- import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.transform.*; import j
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);
}
} }
由于您使用的是org.w3c.dom,因此可以使用以下内容:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlFile));
Document doc = builder.parse(is);
NodeList nodeList = doc.getElementsByTagName("Employee");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasAttributes()) {
Attr attr = (Attr) node.getAttributes().getNamedItem("type");
if (attr != null) {
String attribute= attr.getValue();
System.out.println("attribute: " + attribute);
}
}
}
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
InputSource is=新的InputSource(新的StringReader(xmlFile));
文档doc=builder.parse(is);
NodeList NodeList=doc.getElementsByTagName(“员工”);
对于(inti=0;i
您正在使用的是XSLT转换,默认情况下,它会转换为元素文本
使用相同的技术,需要一个自己的“样式表”:
上面我使用了一个资源,而不是一个文件系统文件,所以它与应用程序打包在一起
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>
参见链接
InputStream xsltIn = StoreData.class.getResourceAsStream("/employees.xslt");
StreamSource xslt = new StreamSource(xsltIn);
///StreamSource xslt = new StreamSource(".../employees.xslt");
Transformer tFormer = TransformerFactory.newInstance().newTransformer(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>
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.