0

I Have a this xml:

<Link ID="6" Name="ALM_JIRA_BUG_2POSICIONGL">
 <Endpoint ordinal="1">
            <type>Micro-Focus-ALM</type>
            <epName>Micro-Focus-ALM</epName>
            <entityName>Defect</entityName>
            <entityType>DEFECT</entityType>
            <ConnectionData>
                <username>sa_aplservmercury</username>
                <password encryptInitString="p2yY5PjzSJs/pn5Kmzz/TQ=="
                    encrypted="true" value="Iwxz/rPt9elfoFxWSqnvfw=="/>
        <Properties>
                    <property name="ServerURL" value="http://alm3.produban.gs.corp/qcbin"/>
                    <property name="Domain" value="QAF_SAN_AGILE"/>
                    <property name="Project" value="PG_ADN360"/>
                </Properties>

            </ConnectionData>

            </Filters>
            <Params/>
        </Endpoint>
</Link>

And these parsing classes

@Data
public class PropertyXml {

    @JacksonXmlProperty(isAttribute = false, localName = "property")
    private String property;

    @JacksonXmlProperty(isAttribute = true, localName = "name")
    private String name;

    @JacksonXmlProperty(isAttribute = true, localName = "value")
    private String value;

}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Properties {

    @JacksonXmlProperty(isAttribute = false, localName = "Properties")
    @JacksonXmlElementWrapper(useWrapping = false)
    List<PropertyXml> properties = new ArrayList<>();
}

@JacksonXmlRootElement(localName = "ConnectionData")
public class ConnectionData {

    @JacksonXmlProperty(localName = "username")
    private String userName;

    @JacksonXmlProperty(localName = "password")
    private Password password;

    @JacksonXmlProperty(localName = "Properties")
    private Properties properties;

}

@JacksonXmlRootElement(localName = "ConnectionData")
public class ConnectionData {

    @JacksonXmlProperty(localName = "username")
    private String userName;

    @JacksonXmlProperty(localName = "password")
    private Password password;

    @JacksonXmlProperty(localName = "Properties")
    private Properties properties;

}


public class Endpoint {

    @JacksonXmlProperty(isAttribute = true, localName = "ordinal")
    private String ordinal;

    @JacksonXmlProperty(isAttribute = false, localName = "type")
    private String type;

    @JacksonXmlProperty(isAttribute = false, localName = "epName")
    private String epName;

    @JacksonXmlProperty(isAttribute = false, localName = "entityName")
    private String entityName;

    @JacksonXmlProperty(isAttribute = false, localName = "entityType")
    private String entityType;

    @JacksonXmlProperty(isAttribute = false, localName = "ConnectionData")
    private ConnectionData connectionData;

    @JacksonXmlProperty(isAttribute = false, localName = "Filters")
    private String filters;

    @JacksonXmlProperty(isAttribute = false, localName = "Params")
    private String params;

}

public class Link {

    @JacksonXmlProperty(isAttribute = true, localName = "ID")
    private String id;

    @JacksonXmlProperty(isAttribute = true, localName = "Name")
    private String name;

    @JacksonXmlProperty(isAttribute = false, localName = "Endpoint")
    private Endpoint endpoint;
}

A través de este código leo el fichero y hago la deserialización:

Service Class:

@Service
public class ConvertToXmlJsonImpl implements ConvertToXmlJsonService {

    public void convertFromXmlFile() {
        File file = new File("C:/incremental.xml");
        XmlMapper xmlMapper = new XmlMapper();
        Link value = null;
        //ConvertToXmlJsonModel value = null;
        String xml = null;
        try {
            xml = inputStreamToString(new FileInputStream(file));
            //value = xmlMapper.readValue(xml, ConvertToXmlJsonModel.class);
            value = xmlMapper.readValue(xml, Link.class);
            System.out.print(value);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //return value;
    }

    private String inputStreamToString(FileInputStream fileInputStream) throws IOException {

        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }

when making the mapper returns an exception :

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "name" (class com.example.convertToXmlJson.model.Properties), not marked as ignorable (one known property: "Properties"]) at [Source: (StringReader); line: 1, column: 567] (through reference chain: com.example.convertToXmlJson.model.Link["Endpoint"]->com.example.convertToXmlJson.model.Endpoint["ConnectionData"]->com.example.convertToXmlJson.model.ConnectionData["Properties"]->java.util.ArrayList[0]->com.example.convertToXmlJson.model.Properties["name"])

1 Answer 1

1

There is two issues about your code :

1) the xml is not correctly formatted, the tag filters has no opening tag:

<Link ID="6" Name="ALM_JIRA_BUG_2POSICIONGL">
 <Endpoint ordinal="1">
            <type>Micro-Focus-ALM</type>
            <epName>Micro-Focus-ALM</epName>
            <entityName>Defect</entityName>
            <entityType>DEFECT</entityType>
            <ConnectionData>
                <username>sa_aplservmercury</username>
                <password encryptInitString="p2yY5PjzSJs/pn5Kmzz/TQ=="
                    encrypted="true" value="Iwxz/rPt9elfoFxWSqnvfw=="/>
        <Properties>
                    <property name="ServerURL" value="http://alm3.produban.gs.corp/qcbin"/>
                    <property name="Domain" value="QAF_SAN_AGILE"/>
                    <property name="Project" value="PG_ADN360"/>
                </Properties>

            </ConnectionData>

            </Filters> <!-- there is no oppening tag for this one-->
            <Params/>
        </Endpoint>
</Link>

2) the mapping for properties should be corrected as follow:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Properties {

    @JacksonXmlProperty(isAttribute = false, localName = "property")
    @JacksonXmlElementWrapper(useWrapping = false)
    List<PropertyXml> properties = new ArrayList<>();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Morning! It is correct, just work. Now, I have an other question:
What is your question about?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.