Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse xml containing namespace through JAXB
    primarykey
    data
    text
    <p>I am trying to parse an xml through JAXB which contains xmlns attribute. If I parse the xml as such it shows <code>NullPointerException</code>. But if I remove namespace tags and xmlns attributes then it worked fine. Sample xml is as follows:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;database xmlns="http://www.Example/Database" xmlns:layout="http://www.Example/Layouter"&gt; &lt;namespace name="X1"&gt; &lt;layout:record name="My_Layout" src="screen1.layout" /&gt; &lt;/namespace&gt; &lt;LayoutGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" LayoutGroupID="ID_1"&gt; &lt;/LayoutGroup&gt; &lt;/database&gt; </code></pre> <p>and my sample code is as follows:</p> <pre><code>@XmlRootElement(name = "database") public class database { private LayoutGroup layoutGrp; @XmlElement(name = "LayoutGroup") public void setLayoutGrp(LayoutGroup gp) { layoutGrp = gp; } public LayoutGroup getLayoutGroup() { return layoutGrp; } } </code></pre> <p>Another class:</p> <pre><code>@XmlRootElement (name="LayoutGroup") public class LayoutGroup { String id; @XmlAttribute (name="LayoutGroupID") public void setId(String id) { this.id = id; } public String getId() { return id; } } </code></pre> <p>Here's my main method:</p> <pre><code>public static void main(String[] args) { database db = JAXB.unmarshal(new File("./res/test.xml"),database.class); System.out.println("Layout id is: "+db.getLayoutGroup().getId()); } </code></pre> <p>Could anyone please help to parse the original xml?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload