Working with namespaces using Spin by Camunda

I work with Spin and I have a problem with namespaces.

Suppose I have the following XML:

<root xmlns="test">
    <sub></sub>
</root>

If I read this in SpinXmlElement using

SpinXmlElement root = Spin.XML("<root xmlns=\"test\"><sub></sub></root>");

<sub>automatically located in the same namespace as that <root>which is the "test".

Now add a new element <sub2>:

SpinXmlElement sub2 = Spin.XML("<sub2></sub2>");
root.append(sub2);

After the xpath test, you can see that it <sub2>does not have a namespace.

try {
    root.xPath("./ns:sub2").ns("ns", "test").element();
} catch (final SpinXPathException ex) {
    System.out.println("This will throw an error, because <sub2> has no namespace.");
}

Note:
If you type rootthrough .toString(), you get the following result:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
   <sub/>
   <sub2 xmlns=""/>
</root>

You can see that it <sub2>has an empty namespace here.

Now add a new item called "sub3". This time with the same namespace as <root>.

SpinXmlElement sub3 = Spin.XML("<sub3 xmlns=\"test\"></sub3>");
root.append(sub3);

xpath-, , <sub3> , <root> ( "test" ).

try {
    root.xPath("./sub3").element();
} catch (final SpinXPathException ex) {
    System.out.println("This will throw an error, because <sub3> has a namespace.");
}

:
, :

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
   <sub/>
   <sub2 xmlns=""/>
   <sub3 xmlns=""/>
</root>

, <sub2> <sub3> , <sub3> <sub2> .

:
1. ? , , . 2. Spin?
3. xmlns ?


.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test">
<!-- All <sub> elements in the namespace "test" -->
   <sub/>
   <sub2/>
   <sub3/>
</root>

.

+4

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


All Articles