下个dom4j包我下面的方法笨了点不知道还有好的不
import javaioFile;
import javautilIterator;
import orgdom4j;
import orgdom4jioSAXReader;
public class ResultXmlTest1 {
public static void main(String args[])
{
SAXReader reader=new SAXReader();
try
{
Document doc=readerread(new File("resultxml"));
Element root=docgetRootElement();
for(Iterator ite=rootelementIterator();itehasNext();)
{
Element ele=(Element)itenext();
for(Iterator iter=eleelementIterator();iterhasNext();)
{
Element elem=(Element)iternext();
//Systemoutprintln(elemgetName());
for(Iterator itera=elemelementIterator();iterahasNext();)
{
Element eleme=(Element)iteranext();
//Systemoutprintln(elemegetName());
for(Iterator iterat=elemeelementIterator();iterathasNext();)
{
Element elemem=(Element)iteratnext();
//Systemoutprintln(elememgetName());
for(Iterator iterato=elememelementIterator();iteratohasNext();)
{
Element elememe=(Element)iteratonext();
//Systemoutprintln(elememegetName());
for(Iterator last=elememeelementIterator();lasthasNext();)
{
Element elemLast=(Element)lastnext();
//Systemoutprintln(elemLastgetName());
for(Iterator at=elemLastattributeIterator();athasNext();)
{
Attribute att=(Attribute)atnext();
if(attgetName()equals("start"))
Systemoutprintln(attgetText());
}
}
}
}
}
}
}
}catch(DocumentException e)
{
eprintStackTrace();
}
}
}
import javaioFile;
import javautilIterator;
import orgdom4jDocument;
import orgdom4jDocumentException;
import orgdom4jElement;
import orgdom4jioSAXReader;
public class ParseXml {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = readerread(new File("d:/testxml"));
Element root = docgetRootElement();
Element record = rootelement("Record");
Iterator<Element> it = recordelementIterator("Field");
while (ithasNext()) {
Element element = itnext();
Element name = elementelement("Name");
Systemoutprintln(namegetText());
Element value = elementelement("Value");
ParseXmlparseValue(value);
}
}
public static void parseValue(Element value) {
Iterator<Element> it = valueelementIterator();
if (ithasNext()) {
while (ithasNext()) {
Element element = itnext();
Element innerField = elementelement("Field");
Element innerName = innerFieldelement("Name");
Systemoutprintln(innerNamegetText());
Element innerValue = innerFieldelement("Value");
parseValue(innerValue);
}
} else {
Systemoutprintln(valuegetText());
}
}
}
dom4j中,使用Elementattributes方法可以获取到节点的属性,而使用elements则可以获取相应的子节点
比如:
Element root = docgetRootElement();
List attrList = rootattributes();
for (int i = 0; i < attrListsize(); i++) {
//属性的取得
Attribute item = (Attribute)attrListget(i);
Systemoutprintln(itemgetName() + "=" + itemgetValue());
}
List childList = rootelements();
for (int i = 0; i < childListsize(); i++) {
//子节点的 *** 作
Element it = (Element) childListget(i);
//对子节点进行其它 *** 作
}
package comapplet;
import javaioByteArrayInputStream;
import javaioFile;
import javaioStringReader;
import javautilIterator;
import javautilList;
import orgapachecommonslangStringUtils;
import orgdom4jDocument;
import orgdom4jDocumentHelper;
import orgdom4jElement;
import orgdom4jioSAXReader;
public class B {
public static void main(String[] args) {
String inputBizInfo = "<xml version=\"10\" encoding=\"utf-8\">\n"
+ "<EAS>\n"
+ " <Header>\n"
+ " <TransID>e688030e-f86f-455b-b143-58871084670b</TransID>\n"
+ " <TransTime>2015-05-29 02:12:15</TransTime>\n"
+ " <DataType>Vendor/Customer</DataType>\n"
+ " <OptCatalog>Single</OptCatalog>\n"
+ " <OptType>Update/Create</OptType>\n"
+ " <RowCount>1</RowCount>\n" + " </Header>\n"
+ " <MasterData>\n" + " <Field name=\"UnitClass\">\n"
+ " <!--单位分类-->2000 单位分类 测试一下\n" + " </Field>\n"
+ " <Field name=\"AccountGroup\">\n" + " <!--账户组-->\n"
+ " </Field>\n" + " <Field name=\"Name1 \">\n"
+ " <!--单位全称-->1111 文具公司\n" + " </Field>\n"
+ " </MasterData>\n" + " <ChangeDetail>\n"
+ " <ChangeFields name=\"Name1\">\n"
+ " <OldValue>文具</OldValue>\n"
+ " <NewValue>皮包</NewValue>\n"
+ " <OperationType>修改</OperationType>\n"
+ " </ChangeFields>\n" + " </ChangeDetail>\n" + "</EAS>";
// 我想获得这个xml中遍历分别获得
// 2000 单位分类 测试一下 ,空, 1111 文具公司 这样的3组值,每组值中的数据 用空格隔开放到数组中去,应该怎么办呢
try {
SAXReader reader = new SAXReader();
String txt = inputBizInforeplaceAll("\n", "");
// Document doc = readerread(new StringReader(txt));
// Document doc = readerread(new
// File("F:\\zz\\FileRecv\\MyWebSocket\\src\\com\\applet\\NewFilexml"));
// Document doc = readerread(new ByteArrayInputStream(txt
// getBytes("UTF-8")));
// Systemerrprintln(txt);
// Systemerrprintln(rootattribute(0)getName());
Document doc = DocumentHelperparseText(txt);
Element root = docgetRootElement();
// Systemoutprintln("Root: " + rootgetName());
List projects = rootselectNodes("MasterData/Field");
// Systemerrprintln(projectssize());
Iterator it = projectsiterator();
while (ithasNext()) {
Element elm = (Element) itnext();
// Systemoutprintln("index:"+elmattributeValue("index")+" level:"+elmattributeValue("level")+" nickname:"+elmattributeValue("nickname")+" country:"+elmattributeValue("country")+" weiwang:"+elmattributeValue("weiwang"));
// Systemerrprintln(elmattributeValue(elmattribute(0)getName()));
// Systemerrprintln(elmgetTextTrim());
String text = elmgetTextTrim();
if (StringUtilsisNotBlank(text)) {
String[] split = textsplit(" ");
for (String string : split) {
//放入数组省略
Systemerrprintln(string);
}
}
}
} catch (Exception ex) {
exprintStackTrace();
}
}
}
以上就是关于如何用java取得XML节点的属性值全部的内容,包括:如何用java取得XML节点的属性值、JAVA解析xml得到节点的值、java中dom4j解析xml文件怎么获取节点属性最好有代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)