How to get the parameter value of an XML element?

I start parsing an XML document and ask the question: how to get the specific value of an XML element parameter in 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> 

The code I write in 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 display the keyword element on the console with the value of the name parameter:

text123

and

text123abc

Please help, thanks.

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

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); 
+5
source

I will tell you how to do it with JAXB.

First of all, your XML is not very well formed. You have filed instead of field in several places.

Valid 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> 

Then go to this site 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 get the xsd schema for your xml file.

Now run xjc sample.xsd (xjc is a tool for creating Java classes for an XML schema, complete with the Java 6 SDK).

You will 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 you get the 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 may seem a bit confusing at startup, but if your XML structure does not change, I will be better off working with typed Java objects than with the DOM itself.

+5
source

The node list contains nodes that are actually instances of subinterfaces (Element, Text, etc.).

This code should work:

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

Source: https://habr.com/ru/post/1336452/


All Articles