实现思路:可以通过w3c的dom解析器进行 *** 作,之后通过getName获取到xmltpye中的属性值。
举例:
import javaioFile;
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNodeList;
public class DomTest1
{
public static void main(String[] args) throws Exception
{
// step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
DocumentBuilderFactory dbf = DocumentBuilderFactorynewInstance();
// Systemoutprintln("class name: " + dbfgetClass()getName());
// step 2:获得具体的dom解析器
DocumentBuilder db = dbfnewDocumentBuilder();
// Systemoutprintln("class name: " + dbgetClass()getName());
// step3: 解析一个xml文档,获得Document对象(根结点)
Document document = dbparse(new File("candidatexml"));
NodeList list = documentgetElementsByTagName("PERSON");
for(int i = 0; i < listgetLength(); i++)
{
Element element = (Element)listitem(i);
String content = elementgetElementsByTagName("NAME")item(0)getFirstChild()getNodeValue();
Systemoutprintln("name:" + content);
Systemoutprintln("--------------------------------------");
}
}
}
#include<iostream>
#include<stdlibh>
#include<stdioh>
static int n=0;
typedef struct tree
{
struct tree left;
int date;
struct tree right;
}treenode,b_tree;
///////插入节点/////////////////////
b_tree insert(b_tree root,int node)
{
b_tree newnode;
b_tree currentnode;
b_tree parentnode;
newnode=(b_tree)malloc(sizeof(treenode));
newnode->date=node;
newnode->right=NULL;
newnode->left=NULL;
if(root==NULL)
return newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->date>node)
currentnode=currentnode->left;
else
currentnode=currentnode->right;
}
if(parentnode->date>node)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
return root;
}
//////建立树///////////////////
b_tree creat(int date,int len)
{
b_tree root=NULL;
int i;
for(i=0;i<len;i++)
root=insert(root,date[i]);
return root;
}
//////前序打印第K个数////////////////
void print3(b_tree root,int k)
{
if(root!=NULL)
{ n++;
if(n==k)
printf("%d\n",root->date);
print3(root->left,k);
print3(root->right,k);
}
}
//////前序打印////////////////
void print(b_tree root)
{
if(root!=NULL)
{
printf("%d->",root->date);
print(root->left);
print(root->right);
}
}
///////测试函数//////////////////
void main()
{
b_tree root=NULL;
int i,index;
int value;
int nodelist[20];
cout<<"输入树的节点,输入0结束\n";
index=0;
cin>>value;
while(value!=0)
{
nodelist[index]=value;
index=index+1;
cin>>value;
}
root=creat(nodelist,index);
printf("\n前序打印\n");
print(root);
printf("\n打印问位置数\n");
cin>>i;
printf("\n前序打印第K个数\n");
print3(root,i); }
代码如下:
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNodeList;
public class FileTest {
/
@param args
/
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactorynewInstance();
try {
DocumentBuilder db = dbfnewDocumentBuilder();
Document doc = dbparse("d:/testxml");
//得到根节点
Element root = docgetDocumentElement();
NodeList nl = rootgetElementsByTagName("id");
Element e = (Element) nlitem(0);
String id=egetTextContent();
Systemoutprintln("id的值为:"+id);
}catch(Exception e){
eprintStackTrace();
}
}
}
上面的代码中我把
<message msgType="response" msgId="String" timestampCreated="2010-5-13 15:55:54" version="String">
<response>
<code>0</code>
<description>成功</description>
</response>
<body bodyId="1">
<id>11223344</id>
</body>
</message>
这段内容放到了D盘下的testxml文件中
你如果要用可以直接将得到的给字符串加载成Document对象,就可以取到了。
给个例子你, 自己去套用就行给你
<xml version="10" encoding="utf-8"><Accounts>
<Account type="type1">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>100000000</money>
</Account>
<Account type="type2">
<code>100002</code>
<pass>123</pass>
<name>张三</name>
<money>100000</money>
</Account>
</Accounts>
java代码解析:
import javaio;/
xml文件解析
@author young
/
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomElement;
import orgw3cdomNode;
import orgw3cdomNodeList;
public class XmlExam {
public static void main(String args[]) {
Element element = null;
// 可以使用绝对路劲
File f = new File("xmlxml");
// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
// 返回documentBuilderFactory对象
dbf = DocumentBuilderFactorynewInstance();
// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象
db = dbfnewDocumentBuilder();
// 得到一个DOM并返回给document对象
Document dt = dbparse(f);
// 得到一个elment根元素
element = dtgetDocumentElement();
// 获得根节点
Systemoutprintln("根元素:" + elementgetNodeName());
// 获得根元素下的子节点
NodeList childNodes = elementgetChildNodes();
// 遍历这些子节点
for (int i = 0; i < childNodesgetLength(); i++) {
// 获得每个对应位置i的结点
Node node1 = childNodesitem(i);
if ("Account"equals(node1getNodeName())) {
// 如果节点的名称为"Account",则输出Account元素属性type
Systemoutprintln("\r\n找到一篇账号 所属区域: "
+ node1getAttributes()getNamedItem("type")
getNodeValue() + " ");
// 获得<Accounts>下的节点
NodeList nodeDetail = node1getChildNodes();
// 遍历<Accounts>下的节点
for (int j = 0; j < nodeDetailgetLength(); j++) {
// 获得<Accounts>元素每一个节点
Node detail = nodeDetailitem(j);
if ("code"equals(detailgetNodeName())) // 输出code
Systemout
println("卡号: " + detailgetTextContent());
else if ("pass"equals(detailgetNodeName())) // 输出pass
Systemout
println("密码: " + detailgetTextContent());
else if ("name"equals(detailgetNodeName())) // 输出name
Systemout
println("姓名: " + detailgetTextContent());
else if ("money"equals(detailgetNodeName())) // 输出money
Systemout
println("余额: " + detailgetTextContent());
}
}
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
//xmlFile是xml文件,nodeName是节点名,attributeName是节点的属性名,因为节点名是可以重复的,所以用list存放返回值
public
List<string>
GetAttribute(string
xmlFile,
string
nodeName,
string
attributeName)
{
List<string>
retList
=
new
List<string>();
XmlDocument
xx
=
new
XmlDocument();
xxLoad(xmlFile);
XmlNodeList
xxList
=
xxGetElementsByTagName(nodeName);
foreach
(XmlNode
xxNode
in
xxList)
{
retListAdd(xxNodeAttributes[attributeName]Value);
}
return
retList;
}
package com;
import javaioStringReader;
import javaxxmlparsersDocumentBuilder;
import javaxxmlparsersDocumentBuilderFactory;
import orgw3cdomDocument;
import orgw3cdomNode;
import orgw3cdomNodeList;
import orgxmlsaxInputSource;
public class TestXML {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("<data>");
sbappend("<message>");
sbappend("<status>6</status>");
sbappend("<value>识别成功</value>");
sbappend("</message>");
sbappend("<cardsinfo>");
sbappend("<card type='6'>");
sbappend("<item desc='保留'><![CDATA[轻轻巧巧]]></item>");
sbappend("<item desc='号牌号码'><![CDATA[888084]]></item>");
sbappend("</card>");
sbappend("</cardsinfo>");
sbappend("</data>");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
newInstance();
DocumentBuilder builder = factorynewDocumentBuilder();
Document doc = builder
parse(new InputSource((new StringReader(sbtoString()))));
NodeList list = docgetElementsByTagName("item");
Systemoutprintln("------------节点item如下:------------");
for (int i = 0; i < listgetLength(); i++) {
Node book = listitem(i);
Systemoutprintln("\t节点=" + i + "\t内容="
+ bookgetFirstChild()getNodeValue());
}
Systemoutprintln("------------结束------------");
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
以上就是关于获取oracle 获取xmltpye节点中的属性值全部的内容,包括:获取oracle 获取xmltpye节点中的属性值、编写递归算法,在二叉树中求位于先序序列中第k个位置的结点的值、java 从XML中取出某个节点的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)