java读取xml信息常用技术有dom解析和dom4J解析
dom4j是最常用的java解析xml技术,在使用时需要下载dom4jjar
具体解析方法可以参考一下内容
xml结构
<books><book id="001">
<title>Harry Potter</title>
<author>J K Rowling</author>
</book>
<book id="002">
<title>Learning XML</title>
<author>Erik T Ray</author>
</book>
</books>
解析为List集合
import javaioFile;import javautilList;
import orgdom4jAttribute;
import orgdom4jDocument;
import orgdom4jElement;
import orgdom4jioSAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
File file = new File("booksxml");
Document document = readerread(file);
Element root = documentgetRootElement();
List<Element> childElements = rootelements();
for (Element child : childElements) {
//未知属性名情况下
/List<Attribute> attributeList = childattributes();
for (Attribute attr : attributeList) {
Systemoutprintln(attrgetName() + ": " + attrgetValue());
}/
//已知属性名情况下
Systemoutprintln("id: " + childattributeValue("id"));
//未知子元素名情况下
/List<Element> elementList = childelements();
for (Element ele : elementList) {
Systemoutprintln(elegetName() + ": " + elegetText());
}
Systemoutprintln();/
//已知子元素名的情况下
Systemoutprintln("title" + childelementText("title"));
Systemoutprintln("author" + childelementText("author"));
//这行是为了格式化美观而存在
Systemoutprintln();
}
}
}
js:
var reg = /<script[^>]>((:(!<\/script>)[\s\S]))<\/script>/gi;
var str = '<script language="javascript">alert(\'邮件发送成功。\');windowopener = null; windowclose(); </script>';
strreplace(reg, "$1");
java:
String reg = "<script[^>]>((:(!<\\/script>)[\\s\\S]))<\\/script>";
String str = "<script language=\"javascript\">alert('邮件发送成功。');windowopener = null; windowclose(); </script>";
Systemoutprintln(strreplaceAll(reg, "$1"));
java读取xml信息常用技术有dom解析和dom4J解析
dom4j是最常用的java解析xml技术,在使用时需要下载dom4jjar
具体解析方法可以参考一下内容
xml结构
<books>
<book id="001">
<title>Harry Potter</title>
<author>J K Rowling</author>
</book>
<book id="002">
<title>Learning XML</title>
<author>Erik T Ray</author>
</book>
</books>
DTD是对xml格式进行规范的,当它不合规范,xml就会飘红,我想,对于你这个题目来说完全没有必要。
<xml version="10" encoding="UTF-8"><librarycards>
<librarycard>
<name>张三</name>
<sex>男</sex>
<studentNO>11111</studentNO>
<cardNO>22222</cardNO>
<faculty>南京某某大学</faculty>
</librarycard>
<librarycard>
<name>李四</name>
<sex>男</sex>
<studentNO>11112</studentNO>
<cardNO>22223</cardNO>
<faculty>南京某某大学</faculty>
</librarycard>
</librarycards>package comlhxtest;
import javaioFile;
import javaioIOException;
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import javaxxmlparsersParserConfigurationException;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNodeList;
import orgxmlsaxSAXException;
public class Test {
public static void main(String[] args) {
DocumentBuilderFactory factory=
DocumentBuilderFactorynewInstance();
try {
DocumentBuilder builder=factorynewDocumentBuilder();
File f=new File("src/com/lhx/test/testxml");
try {
Document doc=builderparse(f);
Element root= docgetDocumentElement();
NodeList list1=rootgetElementsByTagName("name");
NodeList list2=rootgetElementsByTagName("sex");
NodeList list3=rootgetElementsByTagName("studentNO");
NodeList list4=rootgetElementsByTagName("cardNO");
NodeList list5=rootgetElementsByTagName("faculty");
// HashMap<String,Student> map=new HashMap<String, Student>(); 要保存就创建Student
for(int i=0;i<list1getLength();i++){
String name=list1item(i)getTextContent();
Systemoutprintln("\tname:\t"+name);
String sex=list2item(i)getTextContent();
Systemoutprintln("\tsex:\t"+sex);
String studentNO=list3item(i)getTextContent();
Systemoutprintln("\tstudentNo:\t"+studentNO);
String cardNO=list4item(i)getTextContent();
Systemoutprintln("\tcardNo:\t"+cardNO);
String faculty=list5item(i)getTextContent();
Systemoutprintln("\tfaculty:\t"+faculty);
}
} catch (SAXException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
附图:
这是dom解析,,,大概就解析成这样。然后。。。。你需要其他的处理应该不难了。
如果可行的话,望采纳…………^_^
以上就是关于java如何从xml文件中读取一个值全部的内容,包括:java如何从xml文件中读取一个值、正则表达式获取xml标签值、java 怎么读取xml里面的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)