开发者

How to get specific XML element parameter value?

开发者 https://www.devze.com 2023-02-06 12:52 出处:网络
i begin parsing xml document and have question: How to get specific XML element parameter value on Java?

i begin parsing xml document and have question: How to get specific XML element parameter value on Java?

XML document:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<keyword name="text123">
    <profile num="1">
        <url>http://www.a.com</url>
        <field-1 param="">text</field-1>
        <filed-2 param="">text</field-2>
    </profile>
    <profile num="2">
        <url>http://www.b.com</url> 
        <field-1 param="">text</field-1>
        <filed-2 param="">text</field-2>
    </profile>
</keyword>
<keyword name="textabc123">
    <profile num="1">
        <url>http://www.1a.com</url>
        <field-1 param="">text</field-1>
        <filed-2 param="">text</field-2>
    </profile>
    <profile num="2">
        <url>http://www.1b.com</url> 
        <field-1 param="">text</field-1>
        <filed-2 param="">text</field-2>
    </profile>
</keyword>
</data>

code i write on Java:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File xml_file=new File("file.xml");
if (xml_file.isFile() && xml_file.canRead()) 开发者_如何转开发{
 Document doc = builder.parse(xml_file);
 Element root = doc.getDocumentElement();
 NodeList nodel = root.getChildNodes();
 for (int a = 0; a < nodel.getLength(); a++) {
  String data = /* code i don't know to write*/
  System.out.println(data);
 }
} else {}

i want to out to console element "keyword" parameter "name" value:

text123

and

text123abc

Please help, Thanks.


You can use XPath

InputStream is = getClass().getResourceAsStream("somefile.xml");
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = xmlFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(is);
XPathFactory xpathFact = XPathFactory.newInstance();
XPath xpath = xpathFact.newXPath();

String text123 = (String) xpath.evaluate("/data/keyword[1]/@name", xmlDoc, XPathConstants.STRING);
String textabc123 = (String) xpath.evaluate("/data/keyword[2]/@name", xmlDoc, XPathConstants.STRING);


I'll guide you how to do it using JAXB.

First of all, your XML is not well formed. You've got filed instead of field on few places.

Proper XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data>
<keyword name="text123">
    <profile num="1">
        <url>http://www.a.com</url>
        <field-1 param="">text</field-1>
        <field-2 param="">text</field-2>
    </profile>
    <profile num="2">
        <url>http://www.b.com</url> 
        <field-1 param="">text</field-1>
        <field-2 param="">text</field-2>
    </profile>
</keyword>
<keyword name="textabc123">
    <profile num="1">
        <url>http://www.1a.com</url>
        <field-1 param="">text</field-1>
        <field-2 param="">text</field-2>
    </profile>
    <profile num="2">
        <url>http://www.1b.com</url> 
        <field-1 param="">text</field-1>
        <field-2 param="">text</field-2>
    </profile>
</keyword>
</data>

Next, go to this website and download Trang.

Assuming your XML file is named sample.xml, run it through Trang using java -jar trang.jar sample.xml sample.xsd to obtain an xsd schema for your xml file.

Now, run xjc sample.xsd (xjc is a tool for generating Java classes for an XML schema, it's bundled with Java 6 SDK).

You'll get a list of Java classes:

generated\Data.java
generated\Field1.java
generated\Field2.java
generated\Keyword.java
generated\ObjectFactory.java
generated\Profile.java

Put them in your Java project file, and put sample.xml where your program can find it. Now, this is how you get keyword names:

JAXBContext context = JAXBContext.newInstance(Data.class);
Data data = (Data)context.createUnmarshaller().unmarshal(new File("sample.xml"));

List<Keyword> keywords = data.getKeyword();

for (Keyword keyword : keywords) {
    System.out.println(keyword.getName());
}

This method might seem a bit messy at start, but if your XML structure doesn't change, I find it nicer to deal with typed Java objects than with DOM itself.


Node node = nodel.item(a);
if(node instanceof Element) {
  data = ((Element)node).getAttribute("name");
  System.out.println(data);
}


The node list contains nodes, which are in fact instances of subinterfaces (Element, Text, etc.).

This code should thus work :

Node node = nodel.item(a);
if (node instanceof Element) {
    Element e = (Element) node;
    System.out.println(e.getAttribute("name");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消