js中获取标签里面的值除了document.getEelementById()和document.getEelementsByName()之外还有那些方法

js中获取标签里面的值除了document.getEelementById()和document.getEelementsByName()之外还有那些方法,第1张

原生JS获取某节点的方法:

documentgetEelementById(id);//根据id获取此节点

documentgetEelementsByName(name);//根据name获取此节点集

documentgetEelementsByTagName(tagName);//根据标签名获取此节点集

documentgetEelementsByClassName(className);//根据class名称获取此节点集

documentquerySelector(selector);//根据此选择器获取此节点集第一个节点

documentquerySelectorAll(selector);//根据此选择器获取所有此类节点集

你说的是获取指定id下面的节点的属性值吧!

程序如下,其中equal(“”)双引号中的值为节点的属性值

属性值获取如下:

package saxbuilderibmxmlproperty;

import javaioFile;

import javaioIOException;

import javautilIterator;

import orgdom4jAttribute;

import orgdom4jDocument;

import orgdom4jDocumentException;

import orgdom4jElement;

import orgdom4jioSAXReader;

import orgjdomJDOMException;

import orgjdominputSAXBuilder;

public class XmlProperty {

public Element element;

String path = "D:\\xml\\axml";

public void getElementProperty() {

File file = new File(path);

try {

SAXReader reader = new SAXReader();

Document dc = readerread(file);

Element e = dcgetRootElement();

// 节点值

Element child = (Element) egetQName("COMMAND");

Systemoutprintln(childtoString());

// 一级节点

for (Iterator iterator = eelementIterator(); iteratorhasNext();) {

Element el = (Element) iteratornext();

// 一级节点的属性信息

for (Iterator iterator2 = elattributeIterator(); iterator2

hasNext();) {

Attribute attribute = (Attribute) iterator2next();

if(attributegetName()equals("bodyCategory")){

Systemoutprintln("attributegetName()一级节点的属性信息+"+attributegetName()+attributegetValue());

}else{

Systemoutprintln("attributegetName()一级节点的属性信息+"+attributegetName()+attributegetValue());

}

// 二级节点

for (Iterator iterator3 = elelementIterator(); iterator3

hasNext();) {

Element ele = (Element) iterator3next();

// 二级节点的属性信息

for (Iterator iterator4 = eleattributeIterator(); iterator4

hasNext();) {

Attribute attribute1 = (Attribute) iterator4next();

if(attribute1getName()equals("cmdType")){

Systemoutprintln("attribute1getName()二级节点的属性信息+"+attribute1getName()+"+"+attribute1getValue());

}else{

Systemoutprintln("attribute1getName()二级节点的属性信息+"+attribute1getName()+"+"+attribute1getValue());

}

}

}

}

} catch (DocumentException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

var list = documentgetElementById('lList')getElementsByTagName('li')

var list_1 = list[0]innerText

consoledir(list) // Array

consolelog(list_1) // "第一行选择文字1"

(1)parentObjfirstChild:如果节点为已知节点 (parentObj)的第一个子节点就可以使用这个方法。这个属性是可以递归使用的,也就是支持 parentObjfirstChildfirstChildfirstChild的形式,如此就可以获得更深层次的节点。

(2)parentObjlastChild:很显然,这个属性是获取已知节点(parentObj)的最后一个子节点。与firstChild一样,它也可以递归使用。 

在使用中,如果我们把二者结合起来,那么将会达到更加令人兴奋的效果,即:parentObjfirstChildlastChildlastChild

(3)parentObjchildNodes:获取已知节点的子节点数组,然后可以通过循环或者索引找到需要的节点。 

注意:经测试发现,在IE7上获取的是直接子节点的数组,而在Firefox20011上获取的是所有子节点即包括子节点的子节点。

(4)parentObjchildren:获取已知节点的直接子节点数组。 

注意:经测试,在IE7上,和childNodes效果一样,而Firefox20011不支持。这也是为什么我要使用和其他方法不同样式的原因。因此不建议使用。

(5)parentObjgetElementsByTagName(tagName): 使用方法不再赘述,它返回已知节点的所有子节点中类型为指定值的子节点数组。例 如:parentObjgetElementsByTagName('A')返回已知的子节点中的所有超链接。

这个很简单,上代码吧

try {

String strXML = "你的XML字符串";

Document document = DocumentHelperparseText(strXML);

Node node = documentselectSingleNode('examples/example/examplemain/a');

Systemoutprintln(nodegetText());

node = documentselectSingleNode('examples/example/examplemain/b');

Systemoutprintln(nodegetText());

node = documentselectSingleNode('examples/example/examplemain/c');

Systemoutprintln(nodegetText());

} catch (DocumentException ex) {

exprintStackTrace();

}

XmlDocument xml = new XmlDocument();

xmlLoad("RSSChannelXML");//加载文件

XmlNode xmlNode = xmlDocumentElement;//获取根节点

foreach (XmlNode node in xmlNodeChildNodes)//获取第一个子节点

{

TreeNode newNode = treeView1NodesAdd(nodeAttributes[0]ValueToString());//获取节点第1个属性的值

foreach (XmlNode node1 in nodeChildNodes)

{

newNodeNodesAdd(node1Attributes[0]ValueToString());//获取子子节点第1个属性的值

newNodeTag = node1InnerText;

}

}

        XmlDocument xmlDoc = new XmlDocument();

        

        xmlDocLoad(ServerMapPath("XMLFilexml"));

        XmlNode xn = xmlDocSelectSingleNode("root");

        foreach (XmlElement item in xnChildNodes)

        {

            if (item["account"]InnerTextEquals("userName1"))

            {

            }

        }

if里面的句子你如果还不会写,那我也没办法了

import w c dom ;

import javax xml parsers ;

import java io ;

public class Parse{

//Document可以看作是XML在内存中的一个镜像 那么一旦获取这个Document 就意味着可以通过对

//内存的 *** 作来实现对XML的 *** 作 首先第一步获取XML相关的Document

private Document doc=null;

public void init(String xmlFile) throws Exception{

//很明显该类是一个单例 先获取产生DocumentBuilder工厂

//的工厂 在通过这个工厂产生一个DocumentBuilder

//DocumentBuilder就是用来产生Document的

DocumentBuilderFactory dbf=DocumentBuilderFactory newInstance();

DocumentBuilder db=dbf newDocumentBuilder();

//这个Document就是一个XML文件在内存中的镜像

doc=db parse(new File(xmlFile));

}

//该方法负责把XML文件的内容显示出来

public void viewXML(String xmlFile) throws Exception{

this init(xmlFile);

//在xml文件里 只有一个根元素 先把根元素拿出来看看

Element element=doc getDocumentElement();

System out println( 根元素为: +element getTagName());

NodeList nodeList=doc getElementsByTagName( dbstore );

System out println( dbstore节点链的长度: +nodeList getLength());

Node fatherNode=em( );

System out println( 父节点为: +fatherNode getNodeName());

//把父节点的属性拿出来

NamedNodeMap attributes=fatherNode getAttributes();

for(int i= ;i<attributes getLength();i++){

Node attribute=em(i);

System out println( dbstore的属性名为: +attribute getNodeName()+ 相对应的属性值为: +attribute getNodeValue());

}

NodeList childNodes = fatherNode getChildNodes();

System out println(childNodes getLength());

for(int j= ;j<childNodes getLength();j++){

Node childNode=em(j);

//如果这个节点属于Element 再进行取值

if(childNode instanceof Element){

//System out println( 子节点名为: +childNode getNodeName()+ 相对应的值为 +childNode getFirstChild() getNodeValue());

System out println( 子节点名为: +childNode getNodeName()+ 相对应的值为 +childNode getFirstChild() getNodeValue());

}

}

}

public static void main(String[] args)throws Exception{

Parse parse=new Parse();

//我的XML文件

parse viewXML( netct xml );

}

lishixinzhi/Article/program/Java/hx/201311/26710

以上就是关于js中获取标签里面的值除了document.getEelementById()和document.getEelementsByName()之外还有那些方法全部的内容,包括:js中获取标签里面的值除了document.getEelementById()和document.getEelementsByName()之外还有那些方法、java语句如何获取XML文件的节点值、如何用javascript获取id子节点的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9624045.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存