簡體   English   中英

讀取並替換xsl-fo文件中的標簽

[英]read and replace tag in xsl-fo file

我需要閱讀,查找標簽並將其替換為Java中的.fo文件。 請幫我看看如何? 我已經閱讀了一些主題,但是我是Java的新手,並且遇到了一些問題...對於此文件,我必須找到<fo:external-graphic src="url('Images/box.jpg')"/>標記並在其中替換url =“(另一個路徑)”!

 <fo:block font-weight="bold" space-before.optimum="12pt" space-after.optimum="12pt" padding="0.1in" border="thin solid black">
      The manufacturer declines every liability with regard to any direct or 
      consequential damage caused by the equipment to you, your body parts, 
      your personal belongings, your domestic animals e/o beloved relatives. 
      Use this equipment at your own risk, and let God protect your fingers! 
    </fo:block>

    <fo:block space-before.optimum="6pt">Have fun with our stuff!</fo:block>

  </fo:block><fo:block id="d0e81" text-align="justify" font="11pt Times" line-height="1.3" space-before.minimum="18pt" space-before.conditionality="retain">
    <fo:block font="bold 14pt Helvetica" keep-together.within-column="always" keep-with-next.within-column="always" space-before.minimum="6pt" space-before.optimum="12pt" space-before.conditionality="retain" space-after.optimum="3pt" background-color="silver" padding="3pt" border-top="thin solid black" border-bottom="thin solid black">B.  Unpacking &amp; Installing </fo:block>
    <fo:block space-before.optimum="6pt"> 
      The universal hammer comes shipped in a <fo:wrapper font-style="italic" color="blue" rx:key="carton box">carton box</fo:wrapper> 

  (see Fig. 1)
.
    </fo:block><fo:block margin="3pt" border="thin ridge silver" padding="3pt" space-before.optimum="6pt" space-after.optimum="6pt" text-align="center" font-style="italic" font-family="Helvetica" keep-together.within-column="always"><fo:block text-align="center">
    <fo:external-graphic src="url('Images/box.jpg')"/>
    </fo:block>
      Fig. 1.
      Shipping Box.</fo:block> 

創建一個DOM,然后獲取所有名為fo:external-graphic元素。 然后,您可以將src屬性更改為任何您喜歡的屬性。 以下是一些示例代碼,可幫助您入門:

//parse the xml file
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("file.xml"));

//get all elements called fo:external-graphic
NodeList list = doc.getElementsByTagName("fo:external-graphic");
for(int i = 0 ; i < list.getLength() ; i++){
    Element element = (Element)list.item(i);
    NamedNodeMap attributes = element.getAttributes();

    //change the src attribute
    Attr src = (Attr)attributes.getNamedItem("src");
    System.out.println(src.getValue());
    src.setValue("url('another/path/box.jpg')");
}

//let's print out the resulting doc for debugging purposes
OutputFormat format = new OutputFormat(doc);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(doc);
System.out.println(out.toString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM