dom4j.Node iterate maps of elements - java

dom4j.Node to process xml files, and I had a problem with Map-similar structures.
I have followed document structure:
<Party>
<Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
<Address>
</Address>
<Contact>
<ContactInfo>
<Key>IPS</Key>
<Value>null</Value>
<Key>keyTwo</Key>
<Value>1234</Value>
(...)
</ContactInfo>
</Contact>
</Party>
And my goal is to get value for keyTwo element. How to get those element with flexible, non hardcoded way?
My first idea was something like that:
//parent node is father element
Node parentDocument = parentNode.selectSingleNode("ContactInfo");
List<Node> nodesKeys = contactNode.selectNodes("Key");
List<Node> nodesValues = contactNode.selectNodes("Value");
for(int i=0; i<nodesKeys.size(); i++){
if(nodesKeys.get(i).selectSingleNode("Key").equals("keyTwo")){
return nodesValues.get(i);
}
}
But I'm not sure if it is good approach, especially if list of keys and values will be correctly ordered.

Here is a complete working example:
Maven : PoM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sofrecom</groupId>
<artifactId>XmlProcessing</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
Java Main:
package xmlprocessing;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
*
* #author z.benrhouma
*/
public class Main {
public static void main(String... args) {
try {
Document document = parse("src/main/resources/file.xml");
Node node = document.selectSingleNode( "//Party/Contact/ContactInfo/Key[text()='keyTwo']/following-sibling::Value[1]");
System.out.println(node.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document parse(String path) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(path);
return document;
}
}
XML file :
<?xml version="1.0" encoding="UTF-8"?>
<Party>
<Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
<Address>
</Address>
<Contact>
<ContactInfo>
<Key>IPS</Key>
<Value>null</Value>
<Key>keyTwo</Key>
<Value>1234</Value>
</ContactInfo>
</Contact>
</Party>

I think dom4j+Xpath will resolve the problem , you just need to add jaxen dependency.
// ContactInfo Element having key = 'keyTwo'
Node node = document.selectSingleNode( "//Party/Contact/ContactInfo[Key = 'keyTwo']");
// retrieve data from the selected node
String value = node... ;
Maven :
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
Cheers.

Related

GraphStream - No valid display found

I'm following this official tutorial of graphStream.
And I'm trying to run this example code taken from there:
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
public class Tutorial1 {
public static void main(String args[]) {
Graph graph = new SingleGraph("Tutorial 1");
graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addEdge("AB", "A", "B");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");
graph.display();
}
}
And I'm getting the following error:
Exception in thread "main" java.lang.RuntimeException: Cannot launch viewer.
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:212)
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:204)
at org.test.Test.main(Test.java:23)
Caused by: org.graphstream.util.MissingDisplayException: No valid display found. Please check your System.setProperty("org.graphstream.ui") statement.
at org.graphstream.util.Display.getDefault(Display.java:79)
at org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:209)
... 2 more
I did exactly what the tutorial showed.
what am I missing?
what does "No valid display found." means
If you are doing this in a Java application, you need to use version 1.3.
Add the following dependencies. Version 1.3
gs-core
gs-algo
gs-ui
Main
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
public class Main {
public static void main(String args[]) {
Graph graph = new SingleGraph("Tutorial 1");
graph.setStrict(false);
graph.setAutoCreate( true );
graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addEdge("AB", "A", "B");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");
graph.display();
}
}
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sed.home</groupId>
<artifactId>JavaTestArea</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-algo</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-ui</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
</project>
Output
If you use version 2.0, it needs to be done in a JavaFX, Swing, or Android application.

How do I remove the ns2 prefixes from XML elements using JAXB 3 and a NamespacePrefixMapper?

I want to generate a POM file using JAXB 3. I downloaded the XSD https://maven.apache.org/xsd/maven-4.0.0.xsd into src/main/xsd.
I generated the model classes using the JAXB maven plugin.
The POM file of the project is :
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myproject</groupId>
<artifactId>test-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.evolvedbinary.maven.mojohaus</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.apache.maven.pom</packageName>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>
The main class used to generate the sample POM is :
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import org.glassfish.jaxb.core.v2.WellKnownNamespace;
import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws JAXBException {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("project"), Model.class, model);
JAXBContext context = JAXBContext.newInstance(Model.class);
Marshaller m = context.createMarshaller();
m.setProperty("org.glassfish.jaxb.namespacePrefixMapper", new NamespacePrefixMapper() {
#Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return switch (namespaceUri) {
case XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI -> "xsi";
case XMLConstants.W3C_XML_SCHEMA_NS_URI -> "xs";
case WellKnownNamespace.XML_MIME_URI -> "xmime";
default -> "";
};
}
});
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
m.marshal(elem, System.out);
}
}
My issue is, the output contains undesired ns2 prefixes. I didn't figure out how to tell the marshaller that the generated model uses the default namespace and not a namespace named ns2.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns:ns2="http://maven.apache.org/POM/4.0.0">
<ns2:modelVersion>4.0.0</ns2:modelVersion>
<ns2:dependencies>
<ns2:dependency>
<ns2:groupId>commons-io</ns2:groupId>
<ns2:artifactId>commons-io</ns2:artifactId>
<ns2:version>2.11.0</ns2:version>
</ns2:dependency>
</ns2:dependencies>
</project>
I forgot to provide the namespace to the QName :
new QName("http://maven.apache.org/POM/4.0.0", "project")
And now the result looks good :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
There's no need for a prefix mapper, so the code can be simplified:
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXB;
import jakarta.xml.bind.JAXBElement;
public class Main {
public static void main(String[] args) {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("http://maven.apache.org/POM/4.0.0", "project"), Model.class, model);
JAXB.marshal(elem, System.out);
}
}

Cannot resolve symbol 'TestUnit' in maven project

I'm trying to learn JUnit Testing currently. I want to test first custom 'TestUnit' class but IDE tells me that Cannot resolve symbol 'TestUnit'. What I should do to fix this error?
TestUnit class:
package test.lessons.purejava;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestUnit {
#Test
public void testAdd(){
String string = "Test unit works";
assertEquals("Test unit works", string);
}
#Test
public void concatTest(){
Concatenation concatenation = new Concatenation();
String result = concatenation.concat("Hello", " World");
assertEquals("Hello World", result);
}
}
and pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.lessons</groupId>
<artifactId>purejava</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!--<!–
https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
–>-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
</project>

How would I map a complex json file with nested objects to java objects?

I have a complex Json file made up of several nested objects and array of objects. The first object is an "OptionChain" that has an object called "Result". "Result" has nested objects: "Quote" and "Options". Finally, "Options" has nested array of objects named "Call" and "Put".
I have all the class variables annotated with #JSonProperty and using Spring Boot with Jackson to deal with the Object Mapping. I am new with using Jackson and Object Mapping.
When I run the program, I get this error:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "optionChain" (class com.thompson.OptionsImpliedMovement.data.OptionChain), not marked as ignorable (one known property: "result"])
at [Source: (String)"{"optionChain":{"result":[{"underlyingSymbol":"KO","expirationDates":[1550188800,1550793600,1551398400,1552003200,1552608000,1553212800,1553817600,1555545600,1558051200,1561075200,1565913600,1579219200,1610668800],"strikes":[37.0,38.0,40.5,41.5,42.5,43.5,44.5,45.5,46.5,47.5,48.5,49.5,50.5,51.0,51.5,52.0,53.0,53.5,54.0],"hasMiniOptions":false,"quote":{"language":"en-US","region":"US","quoteType":"EQUITY","quoteSourceName":"Nasdaq Real Time Price","currency":"USD","exchangeDataDelayedBy":0,"earnin"[truncated 10817 chars]; line: 1, column: 17] (through reference chain: com.thompson.OptionsImpliedMovement.data.OptionChain["optionChain"])
Here is the main class, pom.xml, and two of my java classes:
Main:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thompson.OptionsImpliedMovement.data.OptionChain;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
#SpringBootApplication
public class OptionsImpliedMovementApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(OptionsImpliedMovementApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
String resourceURL = "https://query2.finance.yahoo.com/v7/finance/options/ko";
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, String.class);
String rawJson = response.getBody();
ObjectMapper objectMapper = new ObjectMapper();
OptionChain optionChain = objectMapper.readValue(rawJson, OptionChain.class);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.thompson</groupId>
<artifactId>OptionsImpliedMovement</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OptionsImpliedMovement</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
OptionChain:
import com.fasterxml.jackson.annotation.JsonProperty;
public class OptionChain {
#JsonProperty("result")
public Result result;
}
Result:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Result {
#JsonProperty("underlyingSymbol")
public String symbol;
#JsonProperty("expirationDates")
public long[] expirationDates;
#JsonProperty("strikes")
public double[] strikes;
#JsonProperty("hasMiniOptions")
public boolean hasMiniOptions;
#JsonProperty("quote")
public Quote quote;
#JsonProperty("options")
public Options option;
}
Here is a screenshot of hierarchy of Json file:
Hierarchy of Json
And finally here is the full Json file attached:
Full Json File
Thanks in advance for any help!
It seems to me that you're unmarshaling JSON that is { "optionChain": {...} } but you're doing so directly into an OptionChain. Instead, you need to define a class that has a single OptionChain member, because you are unmarshaling the outer object that contains this optionChain field (The { } that surrounds the entire response is the object you're trying to unmarshal).
So, for example:
public class OptionChainResponse {
#JsonProperty("optionChain")
private OptionChain optionChain;
// getter/setter
}
And then:
OptionChainResponse optionChainResponse = objectMapper.readValue(rawJson, OptionChainResponse.class);
// do some validation or checking maybe
OptionChain optionChain = optionChainResponse.getOptionChain();

Maven Netbeans and Vget Integration (Can't find Vget from maven)

My porm.xml file looks like this...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
And My java code looks like this, But it is not adding the Vget from the maven to its import list...
package com.mycompany.mavenproject1;
import java.io.File;
import java.net.URL;
public class Downloader {
public static void main(String[] args) {
try {
// ex: http://www.youtube.com/watch?v=Nj6PFaDmp6c
String url = "https://www.youtube.com/watch?v=SCOKysMnH50";
// ex: "/Users/axet/Downloads"
String path = "";
VGet v = new VGet(new URL(url), new File(path));
v.download();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
The Line VGet v = new ... shows an error because the vget is not imported in the code and it is not imported because, the maven won't allow me to import it, I don't know what to do with it...

Categories