Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific XML element parameter value?

Tags:

java

xml

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.

like image 729
user488963 Avatar asked Dec 10 '22 11:12

user488963


2 Answers

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);
like image 140
adrianboimvaser Avatar answered Dec 12 '22 00:12

adrianboimvaser


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.

like image 44
darioo Avatar answered Dec 12 '22 01:12

darioo