如何使用java获取xml节点的属性值
我有一个如下所示的xml:如何使用java获取xml节点的属性值,java,xml,xmlnode,xml-attribute,Java,Xml,Xmlnode,Xml Attribute,我有一个如下所示的xml: { <xml><ep><source type="xml">...</source><source type="text">..</source></ep></xml>} 我也试过: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuil
{ <xml><ep><source type="xml">...</source><source type="text">..</source></ep></xml>}
我也试过:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader("config.xml"));
Document doc = builder.parse(is);
NodeList nodeList = doc.getElementsByTagName("source");
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=newInputSource(newStringReader(“config.xml”);
文档doc=builder.parse(is);
NodeList NodeList=doc.getElementsByTagName(“源”);
对于(inti=0;i
请帮帮我
提前感谢,,
Varsha。因为您的问题更一般,所以请尝试使用Java中可用的XML解析器来实现它。如果您需要特定于解析器的XML解析器,请在此处更新您已经尝试过的代码
试验
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(“uri到xmlfile”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“//ep/source[@type]”);
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i
尝试以下方法:
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
文档dDoc=builder.parse(“d://utf8test.xml”);
XPath=XPathFactory.newInstance().newXPath();
NodeList节点=(NodeList)xPath.evaluate(“//xml/ep/source/@type”,dDoc,XPathConstants.NODESET);
对于(int i=0;i
请注意以下变化:
- 我们需要一个节点集(XPathConstants.nodeset),而不仅仅是一个节点
- xpath现在是//xml/ep/source/@type,而不是//xml/source/@type/text()
PS:你能在你的问题中添加java标记吗?谢谢。我很高兴这段代码运行良好:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("config.xml"));
NodeList nodeList = document.getElementsByTagName("source");
for(int x=0,size= nodeList.getLength(); x<size; x++) {
System.out.println(nodeList.item(x).getAttributes().getNamedItem("type").getNodeValue());
}
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
documentdocument=db.parse(新文件(“config.xml”);
NodeList NodeList=document.getElementsByTagName(“源”);
对于(intx=0,size=nodeList.getLength();x使用
document.getElementsByTagName(“*”)
但是,要从XML文件中获取所有XML元素,这将返回重复属性
例如:
节点列表=doc.getElementsByTagName(“*”);
System.out.println(“XML元素:”)
for(int i=0;ipublicstaticvoidmain(String[]args)抛出IOException{
字符串filePath=“/Users/myXml/VH181.xml”;
文件xmlFile=新文件(文件路径);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
文档生成器dBuilder;
试一试{
dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
打印元素(doc);
System.out.println(“XML文件更新成功”);
}捕获(SAXException | ParserConfiguration异常e1){
e1.printStackTrace();
}
}
私有静态void打印元素(文档节点){
NodeList NodeList=someNode.getElementsByTagName(“choiceInteraction”);
对于(intz=0,size=nodeList.getLength();z,下面是执行此操作的代码
我已经尝试了上面的代码片段,我不想继续,请帮帮我!!我需要单独获取值“xml”和“text”。这是我的要求。ATR,它实际上是//xml/ep/source/@type,我在你的代码中编辑了一些语句。。它工作得很好。。谢谢:)在上面的示例TEST中,我如何读取TEST?您是否尝试过使用VTD-XML,它更快、内存更高效。嗨,Rosdi,不,我没有尝试过,反正ATR的代码工作得很好,我的代码片段也很好:)谢谢您的友好响应,谢谢:)添加了java标签:)你的解释让我明白了…非常感谢!!
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("config.xml"));
NodeList nodeList = document.getElementsByTagName("source");
for(int x=0,size= nodeList.getLength(); x<size; x++) {
System.out.println(nodeList.item(x).getAttributes().getNamedItem("type").getNodeValue());
}
for (int i=0; i<list.getLength(); i++) {
Element element = (Element)list.item(i);
System.out.println(element.getNodeName());
}
public static void main(String[] args) throws IOException {
String filePath = "/Users/myXml/VH181.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
printElement(doc);
System.out.println("XML file updated successfully");
} catch (SAXException | ParserConfigurationException e1) {
e1.printStackTrace();
}
}
private static void printElement(Document someNode) {
NodeList nodeList = someNode.getElementsByTagName("choiceInteraction");
for(int z=0,size= nodeList.getLength();z<size; z++) {
String Value = nodeList.item(z).getAttributes().getNamedItem("id").getNodeValue();
System.out.println("Choice Interaction Id:"+Value);
}
}
import com.ximpleware.*;
public class queryAttr{
public static void main(String[] s) throws VTDException{
VTDGen vg= new VTDGen();
if (!vg.parseFile("input.xml", false))
return false;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("//xml/ep/source/@type");
int i=0;
while((i = ap.evalXPath())!=-1){
system.out.println(" attr val ===>"+ vn.toString(i+1));
}
}
}