XPATH 던지는 java.랭.RuntimeException

0

질문

내가 원하는 CONFIDENTIAL 으로 나의 원하는 출력된다.

내 프로그램

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import java.io.File;
import java.io.IOException;

public class main {
    public static void main(String[] args) {
        File file = new File("test.xml");
        try {
            XmlObject xoobj = XmlObject.Factory.parse(file);
            var declareNameSpaces = "declare namespace v='urn:schemas-microsoft-com:vml' ";
            final String xpathFilter = "data(//v:shape[contains(@id,'PowerPlusWaterMarkObject')]/v:textpath/@string)";

            XmlObject[] arr = xoobj.selectPath(declareNameSpaces + xpathFilter);
            System.out.println(arr[0].toString());
        } catch (XmlException | IOException ex) {
            System.out.println(ex);
        }
    }
}

test.xml

<xml-fragment
        xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
        xmlns:v="urn:schemas-microsoft-com:vml"
        xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
        xmlns:w10="urn:schemas-microsoft-com:office:word"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
        xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
    <w:sdtPr>
        <w:id w:val="-1126775779"/>
        <w:docPartObj>
            <w:docPartGallery w:val="Watermarks"/>
            <w:docPartUnique/>
        </w:docPartObj>
    </w:sdtPr>
    <w:sdtContent>
        <w:p w14:paraId="41319DAD" w14:textId="4534348F" w:rsidR="006868D8" w:rsidRDefault="006868D8">
            <w:pPr>
                <w:pStyle w:val="Header"/>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:noProof/>
                </w:rPr>
                <w:pict w14:anchorId="63C3AA3C">
                    <v:shapetype id="_x0000_t136" coordsize="21600,21600" o:spt="136" adj="10800" path="m@7,l@8,m@5,21600l@6,21600e">
                        <v:formulas>
                            <v:f eqn="sum #0 0 10800"/>
                            <v:f eqn="prod #0 2 1"/>
                            <v:f eqn="sum 21600 0 @1"/>
                            <v:f eqn="sum 0 0 @2"/>
                            <v:f eqn="sum 21600 0 @3"/>
                            <v:f eqn="if @0 @3 0"/>
                            <v:f eqn="if @0 21600 @1"/>
                            <v:f eqn="if @0 0 @2"/>
                            <v:f eqn="if @0 @4 21600"/>
                            <v:f eqn="mid @5 @6"/>
                            <v:f eqn="mid @8 @5"/>
                            <v:f eqn="mid @7 @8"/>
                            <v:f eqn="mid @6 @7"/>
                            <v:f eqn="sum @6 0 @5"/>
                        </v:formulas>
                        <v:path textpathok="t" o:connecttype="custom" o:connectlocs="@9,0;@10,10800;@11,21600;@12,10800" o:connectangles="270,180,90,0"/>
                        <v:textpath on="t" fitshape="t"/>
                        <v:handles>
                            <v:h position="#0,bottomRight" xrange="6629,14971"/>
                        </v:handles>
                        <o:lock v:ext="edit" text="t" shapetype="t"/>
                    </v:shapetype>
                    <v:shape id="PowerPlusWaterMarkObject357476642" o:spid="_x0000_s1025" type="#_x0000_t136" style="position:absolute;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:-251657216;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" o:allowincell="f" fillcolor="silver" stroked="f">
                        <v:fill opacity=".5"/>
                        <v:textpath style="font-family:&quot;Calibri&quot;;font-size:1pt" string="CONFIDENTIAL"/>
                        <w10:wrap anchorx="margin" anchory="margin"/>
                    </v:shape>
                </w:pict>
            </w:r>
        </w:p>
    </w:sdtContent>
</xml-fragment>

출력

/Library/Java/JavaVirtualMachines/jdk-11.0.13.jdk/Contents/Home/bin/java -javaagent:/private/var/folders/5_/5blqbh750wsc9pc01bn0lgph0000gn/T/AppTranslocation/3F078016-ECB8-463C-BEBA-5701DF97E1A5/d/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=54386:/private/var/folders/5_/5blqbh750wsc9pc01bn0lgph0000gn/T/AppTranslocation/3F078016-ECB8-463C-BEBA-5701DF97E1A5/d/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/sahibyar/Dailytasks/test-project/target/classes:/Users/sahibyar/.m2/repository/org/apache/xmlbeans/xmlbeans/3.1.0/xmlbeans-3.1.0.jar:/Users/sahibyar/.m2/repository/org/apache/xmlbeans/xmlbeans-xpath/2.4.0/xmlbeans-xpath-2.4.0.jar:/Users/sahibyar/.m2/repository/net/sf/saxon/saxon/8.5.1/saxon-8.5.1.jar:/Users/sahibyar/.m2/repository/net/sf/saxon/saxon-xpath/8.7/saxon-xpath-8.7.jar:/Users/sahibyar/.m2/repository/net/sf/saxon/saxon-dom/8.7/saxon-dom-8.7.jar main
Exception in thread "main" java.lang.RuntimeException:  Trying XBeans path engine... Trying XQRL... Trying XDK... Trying delegated path engine... FAILED on declare namespace v='urn:schemas-microsoft-com:vml' data(//v:shape[contains(@id,'PowerPlusWaterMarkObject')]/v:textpath/@string)
    at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:217)
    at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:143)
    at org.apache.xmlbeans.impl.store.Cursor._selectPath(Cursor.java:902)
    at org.apache.xmlbeans.impl.store.Cursor.selectPath(Cursor.java:2634)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:476)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:460)
    at main.main(main.java:14)

Process finished with exit code 1

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>org.example</groupId>
    <artifactId>test-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>
java maven xml xpath
2021-11-24 05:17:56
3

최고의 응답

1

다음과 같은 XPath 해결,나의 문제입니다.

final var xpathFilter = "*//v:shape/v:textpath/@string"
2021-11-30 04:20:34
0

declare namespace 에서 수행 할 수 있습 XQuery 지만 XPath 그래서 사용하거나 XQuery 프로세서 다음과 같 BaseX 또는 Saxon10 9 이나 연구의 API 를 선택 XPath 프로세서에 대한 XPath 은 일반적으로있 API 방법에 바인딩하는 접두사를 네임스페이스의 Uri 에 사용할 수 있도록 접두사에서 귀하의 XPath 식입니다.

2021-11-24 08:22:35
0

XMLBeans 지원하지 않는 복잡한 Xpath 기본적으로 포함 data() 방법 및 조건자 필터를 등입니다. 을 지원하는 이러한 기능에 의존 Saxon 에 존재하는 클래스 경로.

에 대한 XMLBeans3.x.x,우리가 해야하는 수동으로 다운로드 슨 9.0.0.4j 추가를 클래스 경로

에 대한 XMLBeans5.x.× (최신),우리가 필요 Saxon-HE 10.x 리 직접 추가할 수 있습 그것은 우리의 pom 다음과 같이 파일

<dependencies>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>5.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>10.6</version>
    </dependency>
</dependencies>

출처: https://xmlbeans.apache.org/documentation/conInstallGuide.html

둘째,우리는 것이 대체하는 필요-수동으로 또는 코드에서 전문 분석-루트 <xml-fragment> 시작과 끝 태그가 가진 어떤 다른 태그 이름 <root> 기 때문에 그렇지 않으면 XMLBeans 구문 분석으로 document fragment 되는 루트 노드를 스키마의 유형을 대신 document 입력하고 Xpath 조건자 작동하지 않습니다.

마지막으로 가정하면,우리는 Saxon 추가 및 처리 <xml-fragment/> 태그입니다. 을 얻 CONFIDENTIAL 값 출력으로,우리는 단지를 캐스팅해야하는 결과 SimpleValue 와 전화 getStringValue()니다.

여기에는 수정

import org.apache.xmlbeans.SimpleValue;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import java.io.File;
import java.io.IOException;

public class main {
    public static void main(String[] args) {
        File file = new File("test.xml");
        try {
            XmlObject xoobj = XmlObject.Factory.parse(file);
            var declareNameSpaces = "declare namespace v='urn:schemas-microsoft-com:vml' ";
            final String xpathFilter = "data(//v:shape[contains(@id,'PowerPlusWaterMarkObject')]/v:textpath/@string)";

            XmlObject[] arr = xoobj.selectPath(declareNameSpaces + xpathFilter);
            
            //Cast to SimpleValue
            SimpleValue result = (SimpleValue) arr[0];
            System.out.println(result.getStringValue());

        } catch (XmlException | IOException ex) {
            System.out.println(ex);
        }
    }
}
2021-11-30 02:44:06

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................